MCPcopy Create free account
hub / github.com/unconed/TermKit / unindent

Function unindent

HTML/external/syntaxhighlighter_3.0.83/src/shCore.js:944–976  ·  view source on GitHub ↗

* Unindents a block of text by the lowest common indent amount. * @param {String} str Text to unindent. * @return {String} Returns unindented text block.

(str)

Source from the content-addressed store, hash-verified

942 * @return {String} Returns unindented text block.
943 */
944function unindent(str)
945{
946 var lines = splitLines(fixInputString(str)),
947 indents = new Array(),
948 regex = /^\s*/,
949 min = 1000
950 ;
951
952 // go through every line and check for common number of indents
953 for (var i = 0; i < lines.length && min > 0; i++)
954 {
955 var line = lines[i];
956
957 if (trim(line).length == 0)
958 continue;
959
960 var matches = regex.exec(line);
961
962 // In the event that just one line doesn't have leading white space
963 // we can't unindent anything, so bail completely.
964 if (matches == null)
965 return str;
966
967 min = Math.min(matches[0].length, min);
968 }
969
970 // trim minimum common number of white space from the begining of every line
971 if (min > 0)
972 for (var i = 0; i < lines.length; i++)
973 lines[i] = lines[i].substr(min);
974
975 return lines.join('\n');
976};
977
978/**
979 * Callback method for Array.sort() which sorts matches by

Callers 1

shCore.jsFile · 0.85

Calls 3

splitLinesFunction · 0.85
fixInputStringFunction · 0.85
trimFunction · 0.85

Tested by

no test coverage detected