Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dgeibi/daify
https://github.com/dgeibi/daify
Last synced: 1 day ago
JSON representation
- Host: GitHub
- URL: https://github.com/dgeibi/daify
- Owner: dgeibi
- License: mit
- Created: 2018-04-19T17:19:22.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-04-19T17:22:06.000Z (over 6 years ago)
- Last Synced: 2024-12-20T03:43:04.764Z (8 days ago)
- Language: JavaScript
- Size: 66.4 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# daify
dictify and arrayify
## API
### opts
#### `Object`
opts.by
-
string
: specify the path to get key of a item, example:`id`
,`n.id`
-
function
: specify the function to get key of a item, example:item => item.id
opts.include[path]
-
opts
for inner object -
path
can have dot likeopts.by
, which specifies the path to a inner array or dict.
#### `string`
shortcut for `opts.by`
### dictify(array, opts)
Make `array` into `dict`
```js
const opts = {
by: "id",
include: {
lists: "id",
"namespace.inners": "id"
}
};
const array = [
{ id: 1, lists: [{ id: 1 }] },
{ id: 2, lists: [{ id: 1 }] },
{
id: 3,
lists: [{ id: 1 }],
namespace: {
inners: [{ id: 1 }, { id: 2 }, { id: 3 }]
}
}
];
log(dictify(array, opts));
/*
{
"1": {
"id": 1,
"lists": {
"1": {
"id": 1
}
}
},
"2": {
"id": 2,
"lists": {
"1": {
"id": 1
}
}
},
"3": {
"id": 3,
"lists": {
"1": {
"id": 1
}
},
"namespace": {
"inners": {
"1": {
"id": 1
},
"2": {
"id": 2
},
"3": {
"id": 3
}
}
}
}
}
*/
```
### arrayify(dict, opts)
Make `dict` into `array`
```js
const opts = {
by: "id", // by is useless here
include: {
lists: "id",
"namespace.inners": "id"
}
};
const dict = {
"1": {
id: 1,
lists: {
"1": { id: 1 }
}
},
"2": {
id: 2,
lists: {
"1": { id: 1 }
}
},
"3": {
id: 3,
lists: { "1": { id: 1 } },
namespace: {
inners: {
"1": { id: 1 },
"2": { id: 2 },
"3": { id: 3 }
}
}
}
};
log(arrayify(dict, opts));
/*
[
{
"id": 1,
"lists": [
{
"id": 1
}
]
},
{
"id": 2,
"lists": [
{
"id": 1
}
]
},
{
"id": 3,
"lists": [
{
"id": 1
}
],
"namespace": {
"inners": [
{
"id": 1
},
{
"id": 2
},
{
"id": 3
}
]
}
}
]
*/
```
## LICENSE
[MIT](LICENSE)