@nexssp/extend
Some functions written/re-written and/or collected to make things easier in JavaScript/NodeJS.
How to Start?
const extend = require('@nexssp/extend')
extend()
extend('array', 'string')
console.log(`All libs: `, extend.libs)
New Object dot notation (no eval!)
{}.dset('a.b.c.d.a.b.a', { x: 1, y: 2 })
const x = {
1: 2,
[',:$']: { w: { eeee: 5 } },
x: 12222,
y: { z: 1345 },
}
console.log(x.dget(',:$.w.eeee'))
New String.addTimestamp and String.trimExtension
'mystring'.addTimestamp()
'myfile.png'.addTimestamp()
'myfile.png'.trimExtension()
New Object.findByProp() and Object.deleteByProp()
Object.findByProp()
{
commands: [
{ name: "a1", "something else": "x" },
{ name: "a2", "something else": "y" },
],
key2: "val2",
}.findByProp("commands", "name", "a2")
Object.deleteByProp()
{
commands: [
{ name: "a1", "something else": "x" },
{ name: "a2", "something else": "y" },
],
key2: "val2",
}.deleteByProp("commands", "name", "a1")
New
[ 'nexss','par1','par with space' ,'--x=a b c' ,"y='a c d'",'a b c' ]
.argvAddQuotes() ==>
[ 'nexss','par1','"par with space"','--x="a b c"','y="a c d"','"a b c"']
New YAML and JSON with 'function' serialize
Yaml
extend('yaml')
const yamlFromObject = {
x: 1,
y: 5,
nested: { x: 5, nested: { t: 'test' } },
}.YAMLstringify()
console.log('YAMLfromObject:', yamlFromObject)
console.log('ObjectFromYaml', yamlFromObject.YAMLparse())
JSON
extend('json')
const x = {
x: 1,
y: function (e) {
console.log(`Hello! ${e}}`)
},
}.JSONstringify()
console.log(x.JSONparse())
New Object functions
extend("object");
{ key1: "val1", key2: "val2" }.invert()
NEW for Strings and Arrays
- argStripQuotes() - "test1" => test2 OR more advanced 'someadvanced="sd asd asd asd=,$$$=!"' => someadvanced=sd asd asd asd=,$$$=!
NEW for Strings
- StripTerminalColors() - removes terminal colors
- StripEndQuotes() - "test1" => test2
Usage
Strings
"mystring='xxx'".argStripQuotes()
'"test2"'.stripEndQuotes()
'Some string'.pad(20, '=')
'abc def'.similarity('abc deg')
'this is a test'.camelCase()
'this ${myvar} and it is ${myvar2}'.template({
myvar: 'works!',
myvar2: 'amazing!',
})
Arrays
[
"myparam1='test1'",
'myparam2="test2"',
'someadvanced="sd asd asd asd=,$$$=!"',
'"Nexss Programmer"="test more adv"',
].argStripQuotes();
["my val1", "my second val",
"and another val"].remove("my second val");
[[1, 2], [3, 4]].flat();
[1, [2, [5, 2], [6]]].flat();
[1, [2, [5, 2, [6]]].flat(2);