Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
CakePHP in Utility.Hash class like a collection manipulation. In JavaScript.
CakePHP in Utility.Hash class like a collection manipulation. In JavaScript.
Work in IE9+
and modern browser. And Node.js.
Array and Object management, if done right, can be a very powerful and useful tool for building smarter, more optimized code. CakeHash offers a very useful set of static utilities in the Hash class that allow you to do just that.
:o: Supports the following methods.
get
extract
insert
remove
combine
check
flatten
expand
map
reduce
:x: Does not support the following methods.
format
contains
filter
merge
numeric
dimensions
maxDimensions
apply
sort
diff
mergeDiff
normalize
nest
Please use underscore.js and lodash.js instead.
$ npm install cake-hash --save
import CakeHash from "cake-hash"
CakeHash.__METHOD__()
script
tag.<script src="cake-hash.min.js"></script>
CakeHash.__METHOD__()
The path syntax described below is used by all the methods in Hash. Not all parts of the path syntax are available in all methods. A path expression is made of any number of tokens. Tokens are composed of two groups. Expressions, are used to traverse the array data, while matchers are used to qualify elements. You apply matchers to expression elements.
Expression | Definition |
---|---|
{n} | Represents a numeric key. Will match any string or numeric key. |
{s} | Represents a string. Will match any string value including numeric string values. |
Foo | Matches keys with the exact same value. |
All expression elements are supported by all methods. In addition to expression elements, you can use attribute matching with certain methods.
Matcher | Definition |
---|---|
[id] | Match elements with a given array key. |
[id=2] | Match elements with id equal to 2. |
[id!=2] | Match elements with id not equal to 2. |
[id>2] | Match elements with id greater than 2. |
[id>=2] | Match elements with id greater than or equal to 2. |
[id<2] | Match elements with id less than 2 |
[id<=2] | Match elements with id less than or equal to 2. |
[text=/.../] | Match elements that have values matching the regular expression inside ... . |
The separator in path syntax can be escaped by using the \
.
Example :
const data = {
"index.html": {
css: {
"style.css": "* {box-sizing: border-box}"
}
}
};
let result = CakeHash.get(data, "index\\.html.css.style\\.css");
console.log(result); // * {box-sizing: border-box}
data
: array | object
path
: string
defaultValue
: mixed
return
: mixed
get()
is a simplified version of extract()
, it only supports direct path expressions. Paths with {n}
, {s}
or matchers are not supported.
Use get()
when you want exactly one value out of an array.
let users = [
{id: 1, name: "mark"},
{id: 2, name: "jane"},
{id: 3, name: "sally"},
{id: 4, name: "jose"}
];
let result = CakeHash.get(users, "2.name");
console.log(result); // "sally"
result = CakeHash.get(users, "hoge.fuga", "default!!");
console.log(result); // default!!
data
: array | object
path
: string
return
: mixed
CakeHash.extract()
supports all expression, and matcher components of Hash Path Syntax.
You can use extract to retrieve data from arrays, along arbitrary paths quickly without having to loop through the data structures.
Instead you use path expressions to qualify which elements you want returned.
let users = [
{id: 1, name: "mark"},
{id: 2, name: "jane"},
{id: 3, name: "sally"},
{id: 4, name: "jose"}
];
let result = CakeHash.extract(users, "{n}.id");
console.log(result); // [1, 2, 3, 4]
data
: array | object
path
: string
value
: mixed
return
: mixed
Inserts data
into an array or object as defined by path
.
let data = {
pages: {name: "page"}
};
let result = CakeHash.insert(data, "files", {name: "file"});
console.log(result);
/*
{
pages: {name: "page"},
files: {name: "file"}
}
*/
You can use paths using {n}
and {s}
to insert data into multiple points.
users = CakeHash.insert(users, "{n}.new", "value");
Attribute matchers work with insert()
as well.
let data = [
{up: true, item: {id: 1, title: "first"}},
{item: {id: 2, title: "second"}},
{item: {id: 3, title: "third"}},
{up: true, item: {id: 4, title: "fourth"}},
{item: {id: 5, title: "fifth"}}
];
let result = CakeHash.insert(data, "{n}[up].item[id=4].new", 9);
console.log(result);
/*
[
{up: true, item: {id: 1, title: "first"}},
{item: {id: 2, title: "second"}},
{item: {id: 3, title: "third"}},
{up: true, item: {id: 4, title: "fourth", new: 9}},
{item: {id: 5, title: "fifth"}}
]
*/
data
: array | object
path
: string
return
: mixed
Removes all elements from an array or object that match path
.
let data = {
pages: {name: "page"},
files: {name: "file"}
};
let result = CakeHash.remove(data, "files");
console.log(result);
/*
{
pages: {name: "page"}
}
*/
Using {n}
and {s}
will allow you to remove multiple values at once. You can also use attribute matchers with remove()
.
let data = [
{clear: true, item: {id: 1, title: "first"}},
{item: {id: 2, title: "second"}},
{item: {id: 3, title: "third"}},
{clear: true, item: {id: 4, title: "fourth"}},
{item: {id: 5, title: "fifth"}}
];
let result = CakeHash.remove(data, "{n}[clear].item[id=4]");
console.log(result);
/*
[
{clear: true, item: {id: 1, title: "first"}},
{item: {id: 2, title: "second"}},
{item: {id: 3, title: "third"}},
{clear: true},
{item: {id: 5, title: "fifth"}}
]
*/
data
: array | object
keyPath
: string
valuePath
: string
groupPath
: string
return
: array | object
Creates an associative array using a keyPath
as the path to build its keys, and optionally valuePath
as path to get the values.
If valuePath
is not specified, or doesn’t match anything, values will be initialized to null
.
You can optionally group the values by what is obtained when following the path specified in groupPath
.
let data = [
{
user: {
id: 2,
group_id: 1,
data: {
user: "mariano.iglesias",
name: "Mariano Iglesias"
}
}
},
{
user: {
id: 14,
group_id: 2,
data: {
user: "phpnut",
name: "Larry E. Masters"
}
}
}
];
let result = CakeHash.combine(data, "{n}.user.id");
console.log(result);
/*
[2: null, 14: null]
*/
result = CakeHash.combine(data, "{n}.user.id", "{n}.user.data");
console.log(result);
/*
[
2: {
user: "mariano.iglesias",
name: "Mariano Iglesias"
},
14: {
user: "phpnut",
name: "Larry E. Masters"
}
]
*/
result = CakeHash.combine(data, "{n}.user.id", "{n}.user.data.name");
console.log(result);
/*
[2: "Mariano Iglesias", 14: "Larry E. Masters"]
*/
result = CakeHash.combine(data, "{n}.user.id", "{n}.user.data", "{n}.user.group_id");
console.log(result);
/*
[
1: {
2: {
name: "Mariano Iglesias",
user: "mariano.iglesias"
}
},
2: {
14: {
name: "Larry E. Masters",
user: "phpnut"
}
}
]
*/
result = CakeHash.combine(data, "{n}.user.id", "{n}.user.data.name", "{n}.user.group_id");
console.log(result);
/*
[
1: {
2: "Mariano Iglesias"
},
2: {
14: "Larry E. Masters"
}
]
*/
data
: array | object
path
: string
return
: boolean
Checks if a particular path is set in an array or object.
let data = {
"My Index 1": {
first: "The first item"
}
};
let result = CakeHash.check(data, "My Index 1.first");
console.log(result); // true
result = CakeHash.check(data, "My Index 1");
console.log(result); // true
data = {
"My Index 1": {
first: {
second: {
third: {
fourth: "Heavy. Nesting."
}
}
}
}
};
result = CakeHash.check(data, "My Index 1.first.second");
console.log(result); // true
result = CakeHash.check(data, "My Index 1.first.second.third");
console.log(result); // true
result = CakeHash.check(data, "My Index 1.first.second.third.fourth");
console.log(result); // true
result = CakeHash.check(data, "My Index 1.first.seconds.third.fourth");
console.log(result); // false
data
: array | object
separator
: string
return
: array | object
Collapses a multi-dimensional array or object into a single dimension.
let data = [
{
post: {id: 1, title: "First Post"},
author: {id: 1, user: "Kyle"}
},
{
post: {id: 2, title: "Second Post"},
author: {id: 3, user: "Crystal"}
}
];
let result = CakeHash.flatten(data);
console.log(result);
/*
{
"0.post.id" : 1,
"0.post.title" : "First Post",
"0.author.id" : 1,
"0.author.user": "Kyle",
"1.post.id" : 2,
"1.post.title" : "Second Post",
"1.author.id" : 3,
"1.author.user": "Crystal"
}
*/
data
: array | object
separator
: string
return
: array | object
Expands an array or object that was previously flattened with CakeHash.flatten()
.
let data = {
"0.post.id" : 1,
"0.post.title" : "First Post",
"0.author.id" : 1,
"0.author.user": "Kyle",
"1.post.id" : 2,
"1.post.title" : "Second Post",
"1.author.id" : 3,
"1.author.user": "Crystal"
};
let result = CakeHash.expand(data);
console.log(result);
/*
[
{
post: {id: 1, title: "First Post"},
author: {id: 1, user: "Kyle"}
},
{
post: {id: 2, title: "Second Post"},
author: {id: 3, user: "Crystal"}
}
]
*/
data
: array | object
path
: string
callback
: function
return
: array | object
Creates a new array or object, by extracting path
, and mapping callback
across the results.
You can use both expression and matching elements with this method.
let data = [
{user: {id: 1, name: "Adam"}},
{user: {id: 2, name: "Clyde"}},
{user: {id: 3, name: "Cyril"}},
{user: {id: 4, name: "Thomas"}},
{user: {id: 5, name: "William"}}
];
let result = CakeHash.map(data, "{n}.user.id", (id) => id * 2);
console.log(result); // [2, 4, 6, 8, 10]
data
: array | object
path
: string
callback
: function
return
: array | object
Creates a single value, by extracting path
, and reducing the extracted results with callback
.
You can use both expression and matching elements with this method.
let data = [
{user: {id: 1, name: "Adam"}},
{user: {id: 2, name: "Clyde"}},
{user: {id: 3, name: "Cyril"}},
{user: {id: 4, name: "Thomas"}},
{user: {id: 5, name: "William"}}
];
let result = CakeHash.reduce(data, "{n}.user.id", (one, two) => one + two);
console.log(result); // 15
Licensed under the MIT license.
Initialization of the project.
$ cd /your/project/dir
$ git clone https://github.com/tsuyoshiwada/cake-hash.git
Install some dependencies.
$ npm install
Start the development.
$ npm start
Run lint and testing.
$ npm test
Generates build file.
$ npm run build
FAQs
CakePHP in Utility.Hash class like a collection manipulation. In JavaScript.
We found that cake-hash 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
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.