Skip to content

Commit

Permalink
Added insert module
Browse files Browse the repository at this point in the history
  • Loading branch information
klaudiosinani committed Nov 17, 2018
1 parent 1df6dd1 commit df8628b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const id = require('./src/id')
const includes = require('./src/includes')
const incr = require('./src/incr')
const init = require('./src/init')
const insert = require('./src/insert')
const isArr = require('./src/is-arr')
const isDate = require('./src/is-date')
const isDef = require('./src/is-def')
Expand Down Expand Up @@ -272,6 +273,7 @@ module.exports = {
includes,
incr,
init,
insert,
isArr,
isDate,
isDef,
Expand Down
20 changes: 20 additions & 0 deletions src/insert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'
const and = require('./and')
const append = require('./append')
const concat = require('./concat')
const curry = require('./curry')
const drop = require('./drop')
const empty = require('./empty')
const notArr = require('./not-arr')
const notStr = require('./not-str')
const reduce = require('./reduce')
const take = require('./take')

function insert(idx, x, xs) {
if (and(notArr(xs), notStr(xs)))
throw new TypeError('[insert] Last argument must be an array or a string')

return reduce(concat, empty(xs), [append(x, take(idx, xs)), drop(idx, xs)])
}

module.exports = curry(insert)

0 comments on commit df8628b

Please sign in to comment.