pylists
v1.2.4
Published
Use List Comprehentions Python in JS (with js functions)
Downloads
10
Maintainers
Readme
Py Lists
Installation
npm i pylists
Usage
Make Python list сomprehension in JavaScript with JavaScript features!!! or it can be deffered array, see more in examples:
Params: new listComprehension(string, array)[for use with real arrays] or new listComprehension(string)
Example
const ListComprehension = require('pylists');
const list1 = new ListComprehension("[x*2 for x in [1, 2, 3, 4, 5, 6]]").exec();
console.log(list1);
// list1 = [ 2, 4, 6, 8, 10, 12 ]
const list2 = new ListComprehension("[x % 2 for x in myList if x === 2]", [0, 1, 2, 3]).exec();
console.log(list2);
// list2 = [ 0 ]
const list3 = new ListComprehension("[x.c.value.f * 2 for x in [{c: {value: {f: 2}}}, {c: {value: 'ac'}}, {c: {value: 'abc3245'}}] if x.c.value.f === 2]").exec();
console.log(list3);
// list3 = [ 4 ]
const list4 = new ListComprehension("[x for x in list]", ['abc', 'def', 'ghi', 'iasdfbcabc'].map(function (a) {
return 'abc' + a + 'abc';
})).exec();
console.log(list4);
// list4 = [ 'abcabcabc', 'abcdefabc', 'abcghiabc', 'abciasdfbcabcabc' ]
const freezedListComprehension = new ListComprehension('[x for x in myList]', [0, 1, 2, 3]);
freezedListComprehension.add(5);
freezedListComprehension.add(...[4, 6, 8]);
//Added AND condition (x !== 4 && x !== 3 && x !== 2)
freezedListComprehension.addAndCondition("x !== 4");
freezedListComprehension.addAndCondition("x !== 3");
freezedListComprehension.addAndCondition("x !== 2");
//Added OR condition (x !== 4 && x !== 3 && x !== 2 || x == 5)
freezedListComprehension.addOrCondition("x == 5");
const list5 = freezedListComprehension.exec();
console.log(list5);
// list5 = [ 0, 1, 5, 6, 8 ]