Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
jsonlang-js
Advanced tools
This package is deprecated. Use @jsonlang/core instead
It is a Typescript package that provides a simple JSON Programming Language, allowing you to execute a safe logic in Frontend or Backend (NodeJS). Furthermore, it can be stored in the database and rendered to the Frontend-Side to execute/run some business logic.
JsonLang is designed to be extendable. You can define new rules with sync/async handlers.
npm install jsonlang-js
{"$R": "R1", "$I": ["value1", "value2", {"$R": "R2", "$I": [...] }, ...] }
. execute = (jsonLang: IJsonLangParams, data?: {}): RuleResult
Execute is used to run the JsonLang and takes two parameters.
Execute is the Sync
version of JsonLang, use it to run all builtin rules and any extended Sync
Rules
executeAsync = async (jsonLang: IJsonLangParams, data?: {}): Promise<RuleResult>
Execute is the Async
version of JsonLang, use it to run all builtin rules and any extended Sync
or Async
Rules
registerOne = (ruleIdentifier: RuleIdentifier, ruleHandler: RuleHandler): void
Extend JsonLang by adding 2 params
{ name: string, shortcut?: string }
, name
(required) is the Rule
name, and shortcut
(optional) is the shortcut. i.e Sum
is the name
, and +
is the shortcut
.(...inputs: RuleInput[]) => RuleResult)
, inputs
(required) is array of all inputs needs for the handler check Input in Structure, and data
is the schemaless data check Data in the Execute SectionregisterMany(rules: Rules): void
registerMany allows registering a Map()
of rules. The Map key
is RuleIdentifier
, and the Map value
is the RuleHandler
JsonLang have three main parameters:
String
) is the rule name itself. i.e. and
, or
, ==
, >
.any[]
) is an array of inputs which will be passed to the Rule
handler/function, their type depends on the Rule
handler, or it can be a nested ruleSymbol [Optional]
), is an optional field, it accept a name of variable which used to save the Rule result in a variable and can be called in any other rule by { "$R": "Var": "$I": ["variableX"] }
. The output value should be unique. If you define the same value more than once, the last one will override the value of the previous one.
Var
Output
from any rules, Check the Output part.Data
["Global"]
it will return the schemaless data object which you pass it to the execute method, else if the input is ["Local"]
, it will return the value passed from the parent rule like filter in array rules.
And or &&
Anding
operation, if any value in Input[]
has a value of (null, 0, false), it will return false
, else it will return true
.Or or ||
Oring
operation, if all values in Input[]
has a value of (null, 0, false), it will return false
, else it will return true
.Equal or ==
Equal
element two or not.NotEqual or =
Not Equal
to element two or not.Not or !
true
it will return false
and vice versa.GreaterThan or >
Greater Than
element two or not.LessThan or <
Less Than
element two or not.GreaterThanOrEqual or >=
Greater Than or Equal
element two or not.LessThanOrEqual or <=
Less Than or Equal
element two or not.
IsNumber
Sum or +
Input1 + Input2 + .... + InputN
.Subtract or -
Input1 - Input2 - .... - InputN
.Multiply or *
Input1 * Input2 * .... * InputN
.Divide or /
Input1 / Input2 / .... / InputN
.
Get [In Progress]
path
must follow the dotted style var1.var2
for nested fields and brackets with number for arrays var1.var2[3].var3
Set [In Progress]
path
must follow the dotted style var1.var2
for nested fields and brackets with number for arrays var1.var2[3].var3
. If the path
does not exist, the Set
Rule will create it.Update [In Progress]
path
must follow the dotted style var1.var2
for nested fields and brackets with number for arrays var1.var2[3].var3
. If the path
does not exist, the Update
rule won't do anything.Delete [In Progress]
path
must follow the dotted style var1.var2
for nested fields and brackets with number for arrays var1.var2[3].var3
. If the path
does not exist, the Delete
rule won't do anything.
All
nested Rules
.Filter
Local
, to access it by the inner rules, you will need to use Data Rule with scope local, check this example.Map
Local
, to access it by the inner rules, you will need to use Data Rule with scope local.Foreach
Local
, to access it by the inner rules, you will need to use Data Rule with scope local.Flatten
import { JsonLang } from 'jsonlang-js';
const jsonLang = new JsonLang();
jsonLang.execute( { "$R": "LessThan" , "$I": [10, 20] } ); // true
// or for short
jsonLang.execute( { "$R": "<" , "$I": [10, 20] } ); // true
// or use the async function
jsonLang.executeAsync( { "$R": "<" , "$I": [10, 20] } )
.then(result => {
console.log(result); // true
});
import { JsonLang } from 'jsonlang-js';
const jsonLang = new JsonLang();
const result = jsonLang.execute({
$R: '+',
$I: [
{
$R: '+',
$I: [
1,
{ $R: '*', $I: [2, 3] },
5
]
},
{
$R: '+',
$I: [
1,
{ $R: '*', $I: [3, 3], $O: 'x' },
5
]
},
{ $R: 'Var', $I: ['x'] },
{ $R: 'Get', $I: ['user.age', null, { $R: 'Data', $I: ['Global'] }] }
]
}, { user: { name: 'test', age: 100 } });
console.log(result);
// 136
import { JsonLang } from 'jsonlang-js';
const jsonLang = new JsonLang();
const result = jsonLang.execute({ $R: 'All', $I: [
{
$R: 'Filter',
$I: [[1, 3, 5], { $R: '>', $I: [{ $R: 'Data', $I: ['Local'] }, 2] }]
},
{
$R: 'Filter',
$I: [
{ $R: 'Get', $I: ['data.test', null, { $R: 'Data', $I: ['Global'] }] },
{ $R: '<', $I: [{ $R: 'Data', $I: ['Local'] }, 500] }
]
}
] }, { data: { id: 'test', test: [100, 300, 700] } });
console.log(result);
// [ [ 3, 5 ], [ 100, 300 ] ]
import { JsonLang } from 'jsonlang-js';
const jsonLang = new JsonLang();
jsonLang.registerOne({ name: 'Test', shortcut: 't' }, (input: any) => {
return `${input} Test`
});
const result = jsonLang.execute({
$R: 'Test',
$I: [
{ $R: 'Get', $I: ['user.age', null, { $R: 'Data' }] }
]
}, { user: { name: 'test', age: 100 } });
console.log(result);
// 100 Test
You can extend JsonLang and add any logic you want from well-known sync/async packages like lodash, moment, ajv, axios, mysql, mongoose, ...etc.
Just use the register functions and follow its structure to add whatever you want.
JsonLang can be extended with any function, and you can override the existing rules, but make sure that any method you will add won't:
This library uses Array.map
and Array.reduce
, so it's not exactly Internet Explorer 8 friendly.
JsonLang is MIT licensed
FAQs
Json Programming Language
The npm package jsonlang-js receives a total of 17,041 weekly downloads. As such, jsonlang-js popularity was classified as popular.
We found that jsonlang-js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.