@jsonlang/array
v0.0.13
Published
JsonLang Extension for Array/Collection Operations
Downloads
79,554
Maintainers
Readme
👋 JsonLang/Array
It is JsonLang Plugin/Extension for Array/Collection operations.
⏬ Installation
npm install @jsonlang/core @jsonlang/array
🎉 Usage
import { JsonLang } from '@jsonlang/core';
import { ArrayRules } from '@jsonlang/array';
const jsonLang = new JsonLang();
jsonLang.import(ArrayRules);
⚒️ Rules
All
- Input[]: Array (Size: Unlimited).
- Output: Array (Size: Unlimited).
- Description: It takes an array of inputs and returns them again. It is used to run a list of
nested Rules
.
Filter
- Input[]: Array (Size: 2) {elements: any[], rule: IJsonLangParams}.
- Output: Any[].
- Description: It accepts array of elements with any type to filter them using nested/inner rules, the filter rule will pass each elements as a Data with scope
Internal
, to access it by the inner rules, you will need to use Data Rule with scope local, check this example.
Map
- Input[]: Array (Size: 2) {elements: any[], rule: IJsonLangParams}.
- Output: Any[].
- Description: It accepts array of elements with any type to map them using nested/inner rules, the filter rule will pass each elements as a Data with scope
Internal
, to access it by the inner rules, you will need to use Data Rule with scope local.
Foreach
- Input[]: Array (Size: 2) {elements: any[], rule: IJsonLangParams}.
- Output: true.
- Description: It accepts array of elements with any type to iterate over them using nested/inner rules, the filter rule will pass each elements as a Data with scope
Internal
, to access it by the inner rules, you will need to use Data Rule with scope local.
Flatten
- Input[]: Array (Size: 2) {elements: any[], level?: number}.
- Output: true.
- Description: It accepts array of elements with any type to flatten this array with any level.
💻Examples
import { JsonLang } from '@jsonlang/core';
import { LogicRules } from '@jsonlang/logic';
import { ArrayRules } from '@jsonlang/array';
import { ObjectRules } from '@jsonlang/object';
const jsonLang = new JsonLang();
jsonLang.import(LogicRules, ArrayRules, ObjectRules);
const result = jsonLang.execute({ $R: 'All', $I: [
{
$R: 'Filter',
$I: [[1, 3, 5], { $R: '>', $I: [{ $R: 'Data', $I: ['Internal'] }, 2] }]
},
{
$R: 'Filter',
$I: [
{ $R: 'Get', $I: ['data.test', null, { $R: 'Data', $I: ['External'] }] },
{ $R: '<', $I: [{ $R: 'Data', $I: ['Internal'] }, 500] }
]
}
] }, { data: { id: 'test', test: [100, 300, 700] } }, { sync: true });
console.log(result);
// [ [ 3, 5 ], [ 100, 300 ] ]
🔌 Compatibility
This library uses Array.map
and Array.reduce
, so it's not exactly Internet Explorer 8 friendly.
📜 License
JsonLang/Array is MIT licensed