https://github.com/charlespascoe/subobject
https://github.com/charlespascoe/subobject
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/charlespascoe/subobject
- Owner: charlespascoe
- License: lgpl-3.0
- Created: 2018-05-25T12:54:53.000Z (about 8 years ago)
- Default Branch: develop
- Last Pushed: 2018-06-01T10:44:53.000Z (about 8 years ago)
- Last Synced: 2025-03-17T00:41:55.413Z (over 1 year ago)
- Language: TypeScript
- Size: 40 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Subobject - pick a subset of keys from an object
Installation:
`$ npm install --save subobject`
## Usage
```ts
import { subobject } from 'subobject';
// An example object
const user = {
id: 123,
firstName: 'Bob',
otherNames: 'Smith',
role: {
name: 'Admin',
permissions: [
{id: 1, 'CAN_CREATE_WIDGETS'},
{id: 2, 'CAN_DELETE_WIDGETS'},
]
}
};
const pickNames = subobject('{firstName, otherNames}');
console.log(pickNames(user));
//{
// firstName: 'Bob',
// otherNames: 'Smith'
//}
const idAndRoleName = subojbect('{id, role: { name }}');
console.log(idAndRole(user));
//{
// id: 123,
// role: {
// name: 'Admin'
// }
//}
const rolePermissions = subobject('{role: { name, permissions: { id }}}');
// For arrays, the filter is applied to each item
console.log(roleAndPermissions(user))
//{
// role: {
// name: 'Admin',
// permissions: [
// {id: 1},
// {id: 2}
// ]
// }
//}
```
## Syntax
```ebnf
whitespace = ? Any whitespace character ? ;
simple_char = 'a'..'z' | 'A'..'Z' | '0'..'9' | '-' | '$' | '_' ;
simple_key = simple_char, { simple_char } ;
complex_key = '"', ? Backslash-escaped string of characters ?, '"' ;
key = simple_key | complex_key ;
expression = key, [ ':', { whitespace }, object ] ;
object = "{", { whitespace } [ expression, { ",", { whitespace }, expression } ], { whitespace }, "}";
```