File tree Expand file tree Collapse file tree 3 files changed +21
-9
lines changed
Expand file tree Collapse file tree 3 files changed +21
-9
lines changed Original file line number Diff line number Diff line change @@ -9,13 +9,13 @@ npm install flat-insert
99```
1010
1111## API
12- ### insert(input, value, [ index] )
13- Returns new ` string ` or ` array ` depending on ` input ` type.
14- * ` input ` : Input value to be modified . (** String** |** Array** )
15- * ` value ` : Any value to be inserted into ` input ` . (** mixed ** )
12+ ### insert(input, value[ , index] )
13+ Returns new modified value depending on ` input ` type.
14+ * ` input ` : Input value. (** String** |** Array** )
15+ * ` value ` : Value to be inserted. (** Mixed ** )
1616 * If ` input ` type is _ String_ , ` value ` will be turned into _ String_ .
1717 * If ` input ` and ` value ` types are _ Array_ , then ` value ` will be flatten.
18- * ` index ` : Index or position at ` input ` where insert ` value ` . (** Number** ; default: ` 0 ` )
18+ * ` index ` : Index position for insertion . (** Number** ; defaults ` 0 ` )
1919
2020### Usage
2121``` javascript
Original file line number Diff line number Diff line change 1- module . exports = function ( res , val , i ) {
2- if ( typeof res !== 'string' && ! Array . isArray ( res ) )
1+ var slice = require ( 'lodash.slice' )
2+ /**
3+ * Returns a new Array or String after inserting and flatten value at index.
4+ * @param {Array|String } input - Input value.
5+ * @param {Mixed } value - Array, String or Number to be inserted.
6+ * @param {Number } index - Index position for insertion.
7+ * @returns {Array|String } New modified value depending on `input` type.
8+ */
9+ module . exports = function ( input , value , index ) {
10+ if ( typeof input !== 'string' && ! Array . isArray ( input ) )
311 throw new TypeError ( 'First argument invalid. Expected Array or String.' )
4- if ( typeof i !== 'undefined' && typeof i !== 'number' )
12+ if ( typeof index !== 'undefined' && typeof index !== 'number' )
513 throw new TypeError ( 'Third argument invalid. Expected Number.' )
6- return res . slice ( 0 , i || 0 ) . concat ( val ) . concat ( res . slice ( i || 0 ) )
14+ return input . slice ( 0 , index || 0 ) . concat ( value ) . concat ( input . slice ( index || 0 ) )
715}
Original file line number Diff line number Diff line change 3737 "devDependencies" : {
3838 "benchmark" : " ^2.1.0" ,
3939 "tape" : " ^4.4.0"
40+ },
41+ "dependencies" : {
42+ "lodash.concat" : " ^4.0.1" ,
43+ "lodash.slice" : " ^4.0.1"
4044 }
4145}
You can’t perform that action at this time.
0 commit comments