marpple / fxts Goto Github PK
View Code? Open in Web Editor NEWA functional programming library for TypeScript/JavaScript
Home Page: https://fxts.dev
License: Apache License 2.0
A functional programming library for TypeScript/JavaScript
Home Page: https://fxts.dev
License: Apache License 2.0
Returns a Iterable/AsyncIterable
of all values of the given object
const iter = values({a:1,b:2,c:3});
iter.next(); // {value:1, done:false}
iter.next(); // {value:2, done:false}
iter.next(); // {value:3, done:false}
iter.next(); // {value:undefined, done:true}
Same logic as the Array.prototype.sort
function, but acceptsIterable
and AsyncIterable
as arguments.
const iter = [1, 2, 3, 4, 5].revers()[Symbol.iterator]();
const asyncIter = toAsync([1, 2, 3, 4, 5].revers()[Symbol.iterator]());
sort((a, b) => a - b, iter); // [1, 2, 3, 4, 5]
await sort((a, b) => a - b, asyncIter); // [1, 2, 3, 4, 5]
const iter = differenceBy(a => a.x, [{ x: 1 }, { x: 4 }], [{ x: 1 }, { x: 2 }, { x: 3 } ])
iter.next(); // {value: {x: 2}, done: false}
iter.next(); // {value: {x: 3}, done: false}
iter.next(); // {value: undefined, done: true}
Returns the given elements in reverse order.
reverse([1,2,3,4,5,6]); // [6,5,4,3,2,1]
reverse('abcde'); // 'edcba'
Consumes the given number of Iterable/AsyncIterable
. If the number is empty, all is consumed.
const iterator = (function *(){
yield 1;
yield 2;
yield 3;
})();
consume(iterator, 2);
iterator.next(); // {value:3, done:false}
// with pipe
pipe(
range(10),
peek(updateApi),
concurrent(5),
consume,
);
Returns the nth element of the given Iterable/AsyncIterable
nth(2, [1,2,3,4]); // 1
nth(5, [1,2,3,4]); // undefined
nth(2, ['name', 'gender', 'age']); // 'age'
nth(3, ['name', 'gender', 'age']); // undefined
nth(2, 'abcdefg'); // 'b'
nth(10, 'abcdefg'); // undefined
There is no contribution guideline yet, so we'd better make it.
example
https://github.com/mattermost/mattermost-server/blob/master/CONTRIBUTING.md
https://github.com/firebase/firebase-js-sdk/blob/master/CONTRIBUTING.md
https://github.com/yorkie-team/yorkie/blob/main/CONTRIBUTING.md
https://reactjs.org/docs/how-to-contribute.html
const add1 = (a: number) => a + 1;
const toUpper = (a: string) => a.toUpperCase();
const obj = { a: 1, b: 'hello', c: true };
evolve({ a: add1, b: toUpper, c: not }, obj);
// { a: 2, b: 'HELLO', c: false }
average([1,2,3,4,5]); // 3
await average(toAsync([1,2,3,4,5])); // 3
Checks if the specified value is equal.
includes('c', 'abcd'); // true
includes(3, [1,2,3,4]); // true
Returns Iterable/AsyncIterable of all elements in the iterable2
not contained in the iterable1
.
const iter = difference([2,1], [2,3]);
iter.next(); // {value: 3, done:false}
iter.next(); // {value: undefined, done: true}
Combines two Iterable
or AsyncIterable
into a set (i.e. no duplicates) composed of those elements common to both Iterable/AsyncIterable
.
const iter = intersection([1,2,3,4], [4,3,5,6]);
iter.next() // {value: 4, done:false};
iter.next() // {value: 3, done:false};
iter.next() // {value: undefined, done:true};
add(1, 2); // 3
await add(1, Promise.resolve(3)); // 4
add('a', 'b'); // 'ab'
await add('a', Promise.resolve('c')); // 'ac'
const products = [
{ id: 1, name: 'T-shirt', price: 50 },
{ id: 2, name: 'Phone case', price: 30 },
{ id: 3, name: 'Key ring', price: 15 },
];
sortBy(a => a.price, products);
ref https://docs.python.org/3/library/itertools.html#itertools.compress
const iter1 = compress([false,true,false,false,true], [1, 2, 3, 4, 5]);
iter1.next(); // {value: 2, done:false}
iter1.next(); // {value: 5, done:false}
iter1.next(); // {value: undefined, done:true }
const iter2 = compress([1,0,0,1,0], "abcde");
iter2.next(); // {value: "a", done:false}
iter2.next(); // {value: "d", done:false}
iter2.next(); // {value: undefined, done:true }
Returns Iterable/AsyncIterable by plucking the same named property off all objects in Iterable/AsyncIterable supplied.
const iter = pluck('age', [{age:21}, {age:22}, {age:23}]);
iter.next(); // 21
iter.next(); // 22
iter.next(); // 23
pickBy(
([key, value]) => key === 'b' || typeof value === 'boolean',
{ a: 1, b: '2', c: true }
);
// { b: '2', c: true }
await pickBy(
async ([key, value]) => key === 'b' || typeof value === 'boolean',
{ a: 1, b: '2', c: true }
);
pipe(
{ a: 1, b: '2', c: true },
pickBy(([key, value]) => key === 'b' || typeof value === 'boolean')
);
await pipe(
{ a: 1, b: '2', c: true },
pickBy(async ([key, value]) => key === 'b' || typeof value === 'boolean')
);
omitBy(
([key, value]) => key === 'b' || typeof value === 'boolean',
{ a: 1, b: '2', c: true }
);
// { a: 1 }
await omitBy(
async ([key, value]) => key === 'b' || typeof value === 'boolean',
{ a: 1, b: '2', c: true }
);
pipe(
{ a: 1, b: '2', c: true },
omitBy(([key, value]) => key === 'b' || typeof value === 'boolean')
);
await pipe(
{ a: 1, b: '2', c: true },
omitBy(async ([key, value]) => key === 'b' || typeof value === 'boolean')
);
apply(Math.max, [1, 2, 3, 4, 5]); // 5
pipe(
repeat(10),
map(a => a * Math.random())
take(5),
apply(max)
);
Returns an Iterable/AsyncIterable
of the given elements from startIndex
(inclusive) to endIndex
(exclusive).
const iter1 = slice(0, 2, [1,2,3,4,5]); // [1, 2]
iter1.next(); // {value:1, done:false}
iter1.next(); // {value:2, done:false}
iter1.next(); // {value:undefined, done:true}
const iter2 = slice(0, 2, 'abcde'); // [1, 2]
iter2.next(); // {value:'a', done:false}
iter2.next(); // {value:'b', done:false}
iter2.next(); // {value:undefined, done:true}
const work = reduce((a, b) => a + b, [1, 2, 3]);
const notWork = reduce((a, b) => a + b, toAsync([1, 2, 3]));
There is a problem that the value of a
is inferred as unknown
when AsyncIterable
type is passed as an argument.
This bug is probably caused by missing some signatures of the reduce
function.
It would be nice to be able to infer the value a
in AsyncIterable
normally.
sum([1,2,3,4,5]); // 15
sum(['a', 'b', 'c']); // 'abc'
await sum(toAsync([1,2,3,4,5])); // 15
await sum(toAsync(['a', 'b', 'c'])); // 'abc'
Returns the index of the first element of Iterable/AsyncIterable which matches f
, or -1
if no element matches.
const arr = [{a:1}, {a:2}, {a:3}]
findIndex((obj) => obj.a === 1, arr); // 0
findIndex((obj) => obj.a === 2, arr); // 1
findIndex((obj) => obj.a === 4, arr); // -1
Returns undefined
noop() // undefined
Applies a list of function to a Iterable/AsyncIterable
of values.
juxt([Math.max, Math.min])(1,2,3,-4,5,6) // [6, -4]
Returns a Iterable/AsyncIterable of size n
containing a specified value.
const iter = repeat(2, 10);
iter.next(); // {value: 10, done:false}
iter.next(); // {value: 10, done:false}
iter.next(); // {value: undefined, done:true}
// with pipe
pipe(
repeat(2, 10),
toArray,
); // [10, 10]
dropWhile(Lazy):
Returns Iterable/AsyncIterable excluding elements dropped from the beginning. Elements are dropped until the value applied to f
returns falsey.
dropUntil(Lazy):
Returns Iterable/AsyncIterable excluding elements dropped from the beginning. Elements are deleted until the value applied to f
returns truly. (It is deleted including the value applied as true.)
const dropWhileIter = dropWhile(a => a < 5, [1,2,3,4,5,6,7,8,9,10]);
dropWhileIter.next(); // 5
dropWhileIter.next(); // 6
dropWhileIter.next(); // 7
dropWhileIter.next(); // 8
dropWhileIter.next(); // 9
dropWhileIter.next(); // 10
const dropUntilIter = dropUntil(a => a > 5, [1,2,3,4,5,6,7,8,9,10]);
dropUntilIter.next(); // 7
dropUntilIter.next(); // 8
dropUntilIter.next(); // 9
dropUntilIter.next(); // 10
Returns an Iterable/AsyncIterable
of key-value pairs for the given object
const iter = entries({a:1,b:2,c:3});
iter.next(); // {value: ['a', 1] , done:false}
iter.next(); // {value: ['b', 2] , done:false}
iter.next(); // {value: ['c', 3] , done:false}
iter.next(); // {value: undefined , done: true}
ref https://docs.python.org/3/library/itertools.html#itertools.cycle
const iter = cycle("abc")
iter.next(); // {value:"a", done: false}
iter.next(); // {value:"b", done: false}
iter.next(); // {value:"c", done: false}
iter.next(); // {value:"a", done: false}
iter.next(); // {value:"b", done: false}
// with pipe
pipe(
cycle([1,2,3,4]),
take(5),
toArray,
); // [1,2,3,4,1]
size(range(10, 16)); // 5
await size(toAsync(range(10, 16))); // 5
min([5, 3, 1]); // 1;
min([5, NaN, 1]); // NaN;
max([1, 3, 5]); // 5;
max([1, NaN, 1]); // NaN;
max([1, Infinity, 1]); // Infinity;
await min(toAsync([5, 3, 1])); // 1;
await max(toAsync([1, 3, 5])); // 5;
Returns Iterable/AsyncIterable out of the two supplied by applying f
to each same positioned pair in Iterable/AsyncIterable.
const iter = zipWith((a,b) => [a,b], [1,2,3], ['a','b','c']);
iter.next(); // {value: [1, 'a'] , done: false}
iter.next(); // {value: [2, 'b'] , done: false}
iter.next(); // {value: [3, 'c'] , done: false}
const ex1 = pipe([1, 2, 3], each(console.log)); // const ex1: void inferred correctly
const ex2 = pipe([1, 2, 3], take(1), each(console.log)); // const ex2: IterableIterator<number> inferred incorrectly
Type of ex2 is inferred IterableIterator<number>
Type of ex2 is expected to be void
.
const iter = intersectionBy((o) => o.x, [{ x: 2 }, { x: 1 }], [{ x: 1 }, {x : 3}, {x : 4}]);
iter.next(); // {value:{x: 1}, done:false}
iter.next(); // {value:undefined, done:true}
I would like to use join
function like other library lodash#join
_.join(['a', 'b', 'c'], '~');
// => 'a~b~c'
All the intended parts are meeting the tests.
By displaying coverage to users, we can make it appear more reliable.
const product = {
name: 'iphone',
price: 1000,
description: '...',
image_url: '...',
};
pick(["name", "price"], product); // { name: 'iphone', price: 1000 }
omit(["name", "price"], product); // { description: '...', image_url: '...' }
https://lodash.com/docs/4.17.15#isObject
Since the purpose of this function is to check only objects, not primitive values, it seems to be implemented in the same way as isObject of lodash.
const iter = scan((acc, cur) => acc * cur, 1, [1, 2, 3, 4 ]);
iter.next(); // {value: 1, done:false}
iter.next(); // {value: 1, done:false}
iter.next(); // {value: 2, done:false}
iter.next(); // {value: 6, done:false}
iter.next(); // {value: 24, done:false}
iter.next(); // {value: undefined, done: true}
curry
is an essential utility for FP, yet most libraries (especially lodash
) fail to provide correct type definition.
Are there any plans for exporting curry
? (since other utilities from fxts
support currying, as I noticed)
const strictEq = curry(<T>(left: T, right: T) => left === right)
const twos = pipe([1, 1, 1, 2, 2, 2], filter(strictEq(2))) // [2, 2, 2]
Splits string by separator. (not support unicode)
const iter = split(',', '1,2,3,4,5,6');
iter.next(); // 1
iter.next(); // 2
iter.next(); // 3
iter.next(); // 4
iter.next(); // 5
iter.next(); // 6
iter.next(); // undefined
Checks if the given value is null
or undefined
.
isNil(1); // false
isNil('1'); // false
isNil(undefined); // true
isNil(null); // true
tc39 also prepares methods for lazy evaluation.
We need to develop the functions being prepared in tc39 in advance.
Additional context
fxts can be evaluated concurrently, and it is also meaningful in that it is provided as a function rather than a method.
If the above proposal(tc39) is reflected, it is likely that our library can be used with native methods. like lodash
Returns true
if the given value is empty value, false
otherwise.
isEmpty(1); // false
isEmpty(0); // false
isEmpty(false); // false
isEmpty(true); // false
isEmpty(new Date()); // false
isEmpty(undefined); // true
isEmpty(null); // true
isEmpty({}); // true
isEmpty({a:1}); // false
isEmpty([]); // true
isEmpty([1]); // false
isEmpty(""); // true
isEmpty("a"); // false
isEmpty(function(){}); // false
isEmpty(Symbol("")); // false
Returns a Iterable/AsyncIterable
containing the own properties of the given object
const iter = keys({a:1, b:2, c:3 });
iter.next(); // {value: 'a', done:false}
iter.next(); // {value:'b', done:false}
iter.next(); // {value:'c', done:false}
iter.next(); // {value:undefined, done:true}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.