src/utils removed
This commit is contained in:
1 parent
729e09d061
commit
23b4b35395
9 files changed
-246
No files matched your search
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}, {});
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
export function query(list) {
|
||||
return list
|
||||
.reduce(
|
||||
(query, param) => query.concat([`${param[0]}=${param[1]}`]),
|
||||
[]
|
||||
)
|
||||
.join('&');
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
);
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
--recursive
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -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');
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user