-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
24 lines (18 loc) · 893 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const numSpaces = (lines) =>
lines.reduce((acc, cur) => {
const indexOfFirstCharacter = cur.search(/[^\s]/)
return ( indexOfFirstCharacter >= 0 && ( acc == 0 || indexOfFirstCharacter < acc ) ) ? indexOfFirstCharacter : acc
}, 0)
function mTrim ( str, startingPad=0 ) { // parameter is for the exporting mtrim function.
if ( Array.isArray(str) ) { str = str[0] } // for tagged template implementation.
if ( typeof str === "number" && this ) { startingPad = str; str = this; }
if ( ! str ) { str = this; } // for prototype implementation.
if ( str ) {
const lines = str.split( '\n' )
const delSpacesNum = numSpaces(lines) - startingPad
return lines.map( (x, index) => (x.search(/[^\s]/) == -1) ? '' : (index > 0) ? x.substring(delSpacesNum) : x ).join('\n')
}
}
String.prototype.mtrim = mTrim;
module.exports = String;
module.exports = { mtrim: mTrim };