UNPKG

fractal-core

Version:

A minimalist and well crafted app, content or component is our conviction

86 lines 3.67 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const ava_1 = require("ava"); const fun_1 = require("./fun"); // Functional utils tests ava_1.default('assoc', t => { let obj = {}; fun_1.assoc('key')('value')(obj); t.is(obj['key'], 'value', 'should assoc a value to a key in an object'); }); ava_1.default('evolve: should apply many functions to the values of an object given the keys', t => { let obj = { count: 0, }; fun_1.evolve({ count: x => x + 1, name: () => 'Fun', })(obj); t.is(obj['count'], 1); t.is(obj['name'], 'Fun'); }); ava_1.default('evolveKey', t => { let obj = { count: 0 }; fun_1.evolveKey('count')(x => x + 1)(obj); t.is(obj['count'], 1, 'should apply a function to a value of an object given the key'); }); ava_1.default('pipe function for piping functions', t => { let fun = fun_1.pipe(x => x + 1, x => x + 1, x => x - 1, x => x * 2); t.is(fun(0), 2); t.is(fun(1), 4); t.is(fun(2), 6); t.is(fun(3), 8); }); ava_1.default('mapToObj helper', t => { t.deepEqual(fun_1.mapToObj([1, 2, 3], (idx, value) => ['a' + idx, `a${value}elm`]), { a0: 'a1elm', a1: 'a2elm', a2: 'a3elm', }); }); ava_1.default('merge helper', t => { t.deepEqual(fun_1.merge({ a: 1, b: 2 })({ c: 3, d: 4 }), { a: 1, b: 2, c: 3, d: 4, }); }); ava_1.default('mapAsync helper', (t) => __awaiter(this, void 0, void 0, function* () { t.deepEqual(yield fun_1.mapAsync([1, 2, 3, 4], (el, i, array) => __awaiter(this, void 0, void 0, function* () { return [el, i]; })), [[1, 0], [2, 1], [3, 2], [4, 3]]); })); ava_1.default('filterAsync helper', (t) => __awaiter(this, void 0, void 0, function* () { t.deepEqual(yield fun_1.filterAsync([1, 2, 3, 4], (el) => __awaiter(this, void 0, void 0, function* () { return el < 3; })), [1, 2]); })); ava_1.default('reduceAsync helper', (t) => __awaiter(this, void 0, void 0, function* () { t.deepEqual(yield fun_1.reduceAsync([1, 2, 3, 4], (ac, el, i) => __awaiter(this, void 0, void 0, function* () { return ac.concat([el, i]); }), []), [1, 0, 2, 1, 3, 2, 4, 3]); })); ava_1.default('all helper', (t) => __awaiter(this, void 0, void 0, function* () { t.deepEqual(yield fun_1.all([1, 2, 3, 4].map(el => Promise.resolve(el))), [1, 2, 3, 4]); })); ava_1.default('range helper', t => { t.deepEqual(fun_1.range(1, 4), [1, 2, 3, 4], 'Ascendant range'); t.deepEqual(fun_1.range(4, -4), [4, 3, 2, 1, 0, -1, -2, -3, -4], 'Descendant range'); }); ava_1.default('sum helper', t => { t.is(fun_1.sum([1, 2, 3, 4]), 10); }); ava_1.default('getPath helper', t => { t.is(fun_1.getPath(['a', 'b', 'c', 'd'], { a: { b: { c: { d: 10 } } } }), 10); }); ava_1.default('getPaths helper', t => { t.deepEqual(fun_1.getPaths([ ['a', 'b', 'c', 'd'], ['z', 'x', 'y', 'w'], ['z', 'x', 'y', 't'], ], { a: { b: { c: { d: 10 } } }, z: { x: { y: { w: 11, t: 12 } } } }), [10, 11, 12]); }); //# sourceMappingURL=fun.spec.js.map