Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/junosuarez/object-match
npm module: match selectors in an object
https://github.com/junosuarez/object-match
Last synced: 8 days ago
JSON representation
npm module: match selectors in an object
- Host: GitHub
- URL: https://github.com/junosuarez/object-match
- Owner: junosuarez
- License: mit
- Created: 2013-04-30T01:03:50.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2013-04-30T06:41:42.000Z (over 11 years ago)
- Last Synced: 2024-04-26T13:20:20.648Z (7 months ago)
- Language: JavaScript
- Size: 98.6 KB
- Stars: 5
- Watchers: 6
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# object-match
match selectors in an object## usage
Let's say you have an object that looks like this:
var obj = {
animals: {
ostrich: {size: 4},
'kangaroo rat': {size: 3},
owl: {size: 2},
'blue whale': {size: 80},
megalodon: {size: 100}
}
}We can't know the keys in advance, but the structure follows a pattern:
'animals.*.size'
We can get the size of all of the animals like so:
var objectMatch = require('object-match')
var sizes = objectMatch('animals.*.size', obj).map(function (match) { return {
name: match.path[1],
size: match.value
})
// => [
// {name: 'ostrich', size: 4},
// {name: 'kangaroo rat', size: 3},
// {name: 'owl', size: 2},
// {name: 'blue whale': size: 80},
// {name: 'megalodon', size: 100}
// ]Matching uses dot notation. Wildcards match any key at that level, and work for either objects or arrays (since array elements are accessible by their index).
You can also supply an array of selectors, which are connected with a logical OR:
objectMatch(['animal.ostrich.size', 'animal.owl.size'], obj).length
// => 2Match objects look like:
{
path: ['animals','owl','size'],
key: 'size',
value: 2,
parent: obj.animals.owl
}In [jsig notation](https://github.com/jden/jsig):
Match: {
path: Array,
key: String,
value: Value,
parent: Object
}`matchObject` is curried. If you don't supply an object, it will return a function with the selectors bound.
## function signature
In [jsig notation](https://github.com/jden/jsig):
matchObject: (selectors: String|Array, obj: Object) => Array
or curried
matchObject: (selectors: String|Array) => (obj: Object) => Array
## installation
$ npm install match-object
## running the tests
From project root:
$ npm install
$ npm test## contributors
jden
## license
MIT. (c) 2013 Agile Diagnosis . See LICENSE.md