Skip to main content
Module

x/rambda/applySpec.spec.js

Faster and smaller alternative to Ramda
Go to Latest
File
import { applySpec as applySpecRamda, nAry } from 'ramda'
import { add, always, compose, dec, inc, map, path, prop, T } from '../rambda'import { applySpec } from './applySpec'
test('different than Ramda when bad spec', () => { const result = applySpec({ sum : { a : 1 } })(1, 2) const ramdaResult = applySpecRamda({ sum : { a : 1 } })(1, 2) expect(result).toEqual({}) expect(ramdaResult).toEqual({ sum : { a : {} } })})
test('works with empty spec', () => { expect(applySpec({})()).toEqual({}) expect(applySpec([])(1, 2)).toEqual({}) expect(applySpec(null)(1, 2)).toEqual({})})
test('works with unary functions', () => { const result = applySpec({ v : inc, u : dec, })(1) const expected = { v : 2, u : 0, } expect(result).toEqual(expected)})
test('works with binary functions', () => { const result = applySpec({ sum : add })(1, 2) expect(result).toEqual({ sum : 3 })})
test('works with nested specs', () => { const result = applySpec({ unnested : always(0), nested : { sum : add }, })(1, 2) const expected = { unnested : 0, nested : { sum : 3 }, } expect(result).toEqual(expected)})
test('works with arrays of nested specs', () => { const result = applySpec({ unnested : always(0), nested : [ { sum : add } ], })(1, 2)
expect(result).toEqual({ unnested : 0, nested : [ { sum : 3 } ], })})
test('works with arrays of spec objects', () => { const result = applySpec([ { sum : add } ])(1, 2)
expect(result).toEqual([ { sum : 3 } ])})
test('works with arrays of functions', () => { const result = applySpec([ map(prop('a')), map(prop('b')) ])([ { a : 'a1', b : 'b1', }, { a : 'a2', b : 'b2', }, ]) const expected = [ [ 'a1', 'a2' ], [ 'b1', 'b2' ], ] expect(result).toEqual(expected)})
test('works with a spec defining a map key', () => { expect(applySpec({ map : prop('a') })({ a : 1 })).toEqual({ map : 1 })})
test('cannot retains the highest arity', () => { const f = applySpec({ f1 : nAry(2, T), f2 : nAry(5, T), }) const fRamda = applySpecRamda({ f1 : nAry(2, T), f2 : nAry(5, T), }) expect(f.length).toBe(0) expect(fRamda.length).toBe(5)})
test('returns a curried function', () => { expect(applySpec({ sum : add })(1)(2)).toEqual({ sum : 3 })})
// Additional tests// ============================================test('arity', () => { const spec = { one : x1 => x1, two : (x1, x2) => x1 + x2, three : ( x1, x2, x3 ) => x1 + x2 + x3, } expect(applySpec( spec, 1, 2, 3 )).toEqual({ one : 1, two : 3, three : 6, })})
test('arity over 5 arguments', () => { const spec = { one : x1 => x1, two : (x1, x2) => x1 + x2, three : ( x1, x2, x3 ) => x1 + x2 + x3, four : ( x1, x2, x3, x4 ) => x1 + x2 + x3 + x4, five : ( x1, x2, x3, x4, x5 ) => x1 + x2 + x3 + x4 + x5, } expect(applySpec( spec, 1, 2, 3, 4, 5 )).toEqual({ one : 1, two : 3, three : 6, four : 10, five : 15, })})
test('curried', () => { const spec = { one : x1 => x1, two : (x1, x2) => x1 + x2, three : ( x1, x2, x3 ) => x1 + x2 + x3, } expect(applySpec(spec)(1)(2)(3)).toEqual({ one : 1, two : 3, three : 6, })})
test('curried over 5 arguments', () => { const spec = { one : x1 => x1, two : (x1, x2) => x1 + x2, three : ( x1, x2, x3 ) => x1 + x2 + x3, four : ( x1, x2, x3, x4 ) => x1 + x2 + x3 + x4, five : ( x1, x2, x3, x4, x5 ) => x1 + x2 + x3 + x4 + x5, } expect(applySpec(spec)(1)(2)(3)(4)(5)).toEqual({ one : 1, two : 3, three : 6, four : 10, five : 15, })})
test('undefined property', () => { const spec = { prop : path([ 'property', 'doesnt', 'exist' ]) } expect(applySpec(spec, {})).toEqual({ prop : undefined })})
test('restructure json object', () => { const spec = { id : path('user.id'), name : path('user.firstname'), profile : path('user.profile'), doesntExist : path('user.profile.doesntExist'), info : { views : compose(inc, prop('views')) }, type : always('playa'), }
const data = { user : { id : 1337, firstname : 'john', lastname : 'shaft', profile : 'shaft69', }, views : 42, }
expect(applySpec(spec, data)).toEqual({ id : 1337, name : 'john', profile : 'shaft69', doesntExist : undefined, info : { views : 43 }, type : 'playa', })})