From 23b4b35395b8caf582861a7dd0a0ffa79b245406 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Tue, 3 Mar 2020 19:07:27 +0300 Subject: [PATCH] src/utils removed --- src/utils/common.ts | 69 ------------------------------------------- src/utils/fn.js | 13 -------- src/utils/net.js | 8 ----- src/utils/populate.js | 35 ---------------------- src/utils/sort.js | 26 ---------------- src/utils/string.js | 22 -------------- test/mocha.opts | 1 - test/utils/common.js | 51 -------------------------------- test/utils/net.js | 21 ------------- 9 files changed, 246 deletions(-) delete mode 100644 src/utils/common.ts delete mode 100644 src/utils/fn.js delete mode 100644 src/utils/net.js delete mode 100644 src/utils/populate.js delete mode 100644 src/utils/sort.js delete mode 100644 src/utils/string.js delete mode 100644 test/mocha.opts delete mode 100644 test/utils/common.js delete mode 100644 test/utils/net.js diff --git a/src/utils/common.ts b/src/utils/common.ts deleted file mode 100644 index b06647d2..00000000 --- a/src/utils/common.ts +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Return a closure that compares a Number argument with specified Number - * @param withNumber Number to compare with - * @returns {Function} (i) => {} - */ -export function numberEquals(withNumber) { - return i => i === withNumber; -} - -/** - * Return a closure that compares a second Number argument with specified Number - * @param withNumber Number to compare with - * @returns {Function} (i) => {} - */ -export function indexEquals(withNumber) { - return (_, i) => i === withNumber; -} - -/** - * Return a reduce function - * [['spring', 'Весена'], ['autumn', 'Осень']] -> 'Весна' or 'Осень' depend on byCondition result - * @param byCondition - * @param map - */ -export function select(byCondition, map = i => i) { - return (result, item) => byCondition(item) ? map(item) : map(result) -} - -/** - * Select an item in Array by specified selector function - * @param scope - * @param selector - * @returns {*} - */ -export function choise(scope, selector) { - return scope.reduce(selector, null); -} - -export function assign(dict, key, value) { - dict[key] = value; - return dict; -} - -export function map(v, fn) { - return value => fn(value) ? v : value; -} - -/** - * - * f(g, [x, y, z, ...]) => map of {g(x): x, g(y): y, g(z): z} - * - * @param list - * @param selector - * @returns {*} - */ -export function createMap(selector, list) { - return list.reduce((map, i) => map.set(selector(i), i), new Map()); -} - -/** - * - * f(g) => x ? g(x) : null - * - * @param {function} fn - * @returns {function(*=): null} - */ -export function nullSafe(fn) { - return i => i ? fn(i) : null; -} diff --git a/src/utils/fn.js b/src/utils/fn.js deleted file mode 100644 index caaf894a..00000000 --- a/src/utils/fn.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * - * f([x, y, z, ...]) -> f(v) -> {v.x, v.y, v.z} - * - * @param fields - * @returns {function(*): *} - */ -export function select(fields=[]){ - return i => fields.reduce((out, key) => { - out[key] = i[key]; - return out; - }, {}); -} diff --git a/src/utils/net.js b/src/utils/net.js deleted file mode 100644 index 623cab68..00000000 --- a/src/utils/net.js +++ /dev/null @@ -1,8 +0,0 @@ -export function query(list) { - return list - .reduce( - (query, param) => query.concat([`${param[0]}=${param[1]}`]), - [] - ) - .join('&'); -} diff --git a/src/utils/populate.js b/src/utils/populate.js deleted file mode 100644 index d5a50261..00000000 --- a/src/utils/populate.js +++ /dev/null @@ -1,35 +0,0 @@ -export function pop(data){ - var Populator = function(data){ - this.target = data; - } - - Populator.prototype = { - populate: function(p){ - var self = this; - if(typeof p == "function"){ - self.target = p(self.target); - }else if(p instanceof Array){ - p.forEach(function(i){ - self.target = self.populate(i).result(); - }); - - } - return self; - }, - result: function(){ - return this.target; - } - }; - - var fun = new Populator(data); - return fun; -} - -export function populate(fn, key){ - return (dict) => { - let id = dict[key]; - let value = fn(id); - dict[key] = value ? value : id; - return dict; - } -} \ No newline at end of file diff --git a/src/utils/sort.js b/src/utils/sort.js deleted file mode 100644 index 774b1adf..00000000 --- a/src/utils/sort.js +++ /dev/null @@ -1,26 +0,0 @@ -export function sortByPattern(list, pattern, map) { - let weight = i => { - const index = pattern.indexOf(map(i)) - return index > -1 ? pattern.length - index : 0 - } - - return sortByWeight(list, weight) -} - -export function sortByWeight(list, fn) { - return list - .map(i => [fn(i), i]) - .sort((a, b) => b[0] - a[0]) - .map(i => i[1]) -} - -export function sortBy(fn) { - return (i1, i2) => { - let t1 = fn(i1) - let t2 = fn(i2) - - if (t1 < t2) return 1 - if (t1 > t2) return -1 - return 0 - } -} diff --git a/src/utils/string.js b/src/utils/string.js deleted file mode 100644 index a5e7dace..00000000 --- a/src/utils/string.js +++ /dev/null @@ -1,22 +0,0 @@ -import {map} from './common'; - -/** - * Return a function that accept one String parameter and returns result of replacing. - * - * @param searchValue - * @param replaceValue - */ -export const stringReplace = (searchValue, replaceValue) => - str => str.replace(searchValue, replaceValue); - -let toTrue = map(true, i => i === 'true'); -let toFalse = map(false, i => i === 'false'); -const boolProcessors = [toTrue, toFalse]; - -export function stringToBoolean(string){ - return boolProcessors - .reduce( - (value, fn) => fn(value), - string - ); -} \ No newline at end of file diff --git a/test/mocha.opts b/test/mocha.opts deleted file mode 100644 index 63b406d2..00000000 --- a/test/mocha.opts +++ /dev/null @@ -1 +0,0 @@ ---recursive \ No newline at end of file diff --git a/test/utils/common.js b/test/utils/common.js deleted file mode 100644 index db59b9f7..00000000 --- a/test/utils/common.js +++ /dev/null @@ -1,51 +0,0 @@ -import should from 'should'; -import {select} from '../../src/utils/common'; - -describe('utils', () => { - describe('common.js', () => { - describe('selectItem', () => { - it('should return a function', () => { - let fn = select(); - fn.should.be.type('function'); - }); - - it('should accept a function as a first parameter and use it as byCondition', () => { - let sample = [[1235, 'hello'], [2352, 'banana']]; - - let fn = select( - i => i[0] === 2352 && i[1] === 'banana' && i.length === 2, - i => i[1] - ); - - let result = sample.reduce(fn); - result.should.be.equal('banana'); - }); - - it('should accept a function as a second parameter and use it as mapping function', () => { - let sample = [[1235, 'hello'], [2352, 'banana']]; - - let fn = select( - i => i[0] === 2352, - i => i[1] - ); - - let result = sample.reduce(fn); - result.should.be.equal('banana'); - }); - - it('should pass with one parameter', () => { - let sample = ['hello', 'lol', 'banana']; - - let result = sample.reduce(select(i => i === 'banana')); - result.should.be.equal('banana'); - }); - - it('should get a null in reduce result if reduce calls with second parameter that is null', () => { - let sample = ['hello', 'lol', 'banana']; - - let result = sample.reduce(select(i => i === 'apple'), null); - should.not.exist(result); - }); - }); - }); -}); \ No newline at end of file diff --git a/test/utils/net.js b/test/utils/net.js deleted file mode 100644 index fe4a9807..00000000 --- a/test/utils/net.js +++ /dev/null @@ -1,21 +0,0 @@ -import should from 'should'; -import {query} from '../../src/utils/net'; - -describe('utils', () => { - describe('net.js', () => { - describe('query', () => { - let sample = [['count', 10], ['portion', 5], ['hello', 'lol']]; - - it('should combine query params', () => { - let params = query(sample); - params.should.be.equal('count=10&portion=5&hello=lol'); - }); - - it('should return string', () => { - let params = query(sample); - - params.should.be.type('string'); - }); - }); - }); -}); \ No newline at end of file