updeep-remeda/test/map-spec.js

76 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-04-19 00:55:46 +00:00
import { expect } from 'chai'
import u from '../lib'
2015-08-05 07:25:34 +00:00
describe('u.map', () => {
it('applies updates to each item in an array', () => {
2017-04-19 00:55:46 +00:00
const object = [0, 1, 2]
2020-04-02 14:16:13 +00:00
const inc = (x) => x + 1
2017-04-19 00:55:46 +00:00
const result = u.map(inc, object)
2015-08-05 07:25:34 +00:00
2017-04-19 00:55:46 +00:00
expect(result).to.eql([1, 2, 3])
})
2015-08-05 07:25:34 +00:00
it('applies updates to each value in an object', () => {
2017-04-19 00:55:46 +00:00
const object = { a: 0, b: 1, c: 2 }
2020-04-02 14:16:13 +00:00
const inc = (x) => x + 1
2017-04-19 00:55:46 +00:00
const result = u.map(inc, object)
2015-08-05 07:25:34 +00:00
2017-04-19 00:55:46 +00:00
expect(result).to.eql({ a: 1, b: 2, c: 3 })
})
2015-08-05 07:25:34 +00:00
it('can update with a regular updates object', () => {
2017-04-19 00:55:46 +00:00
const object = [{ a: 0 }, { a: 0 }]
const result = u.map({ a: 1 }, object)
2015-08-05 07:25:34 +00:00
2017-04-19 00:55:46 +00:00
expect(result).to.eql([{ a: 1 }, { a: 1 }])
})
2015-08-05 07:25:34 +00:00
it('returns the same object if no updates are made', () => {
2017-04-19 00:55:46 +00:00
const array = [0, 1]
2020-04-02 14:16:13 +00:00
const ident = (x) => x
2017-04-19 00:55:46 +00:00
let result = u.map(ident, array)
2017-04-19 00:55:46 +00:00
expect(result).to.equal(array)
2017-04-19 00:55:46 +00:00
const object = { a: 0 }
result = u.map(ident, object)
2017-04-19 00:55:46 +00:00
expect(result).to.equal(object)
})
2015-08-05 07:25:34 +00:00
it('passes the key or index as the second parameter to the iteratee', () => {
2015-08-05 07:27:56 +00:00
const object = {
2015-08-05 07:25:34 +00:00
a: { x: 0 },
b: [3, 3],
2017-04-19 00:55:46 +00:00
}
const setToKey = (_, key) => key
const result = u.map(u.map(setToKey), object)
2015-08-05 07:25:34 +00:00
2016-01-13 16:19:43 +00:00
expect(result).to.eql({
2015-08-05 07:25:34 +00:00
a: { x: 'x' },
b: [0, 1],
2017-04-19 00:55:46 +00:00
})
})
2015-08-05 07:25:34 +00:00
it('can be partially applied', () => {
2015-08-05 07:27:56 +00:00
const object = {
2015-08-05 07:25:34 +00:00
b: [3, 3],
2017-04-19 00:55:46 +00:00
}
const setToKey = (_, key) => key
const result = u(
{
b: u.map(setToKey),
},
object
)
2015-08-05 07:25:34 +00:00
2016-01-13 16:19:43 +00:00
expect(result).to.eql({
2015-08-05 07:25:34 +00:00
b: [0, 1],
2017-04-19 00:55:46 +00:00
})
})
it('freezes the result', () => {
2017-04-19 00:55:46 +00:00
expect(Object.isFrozen(u.map({}, {}))).to.be.true
})
})