Skip to content

Commit e2632de

Browse files
committed
update docs and naming
1 parent 8d37cf6 commit e2632de

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff 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

index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
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
}

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,9 @@
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
}

0 commit comments

Comments
 (0)