Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/massivefermion/hays

find the dot path and value of a key of a specific name or pattern deep inside an object and transform or replace it
https://github.com/massivefermion/hays

Last synced: about 2 months ago
JSON representation

find the dot path and value of a key of a specific name or pattern deep inside an object and transform or replace it

Awesome Lists containing this project

README

        

# hays

find the dot path and value of a key of a specific name or pattern deep inside an object and transform or replace it.

## Install

`npm install hays`

## Usage

```javascript
const hays = require('hays')

const book = {
isbn: '123-456-222',
title: 'The Ultimate Database Study Guide',
category: ['Non-Fiction', 'Technology'],
author: {
lastname: 'Windler',
firstname: 'Dianne',
},
editor: {
lastname: 'Smith',
firstname: 'Felicity',
},
}

hays.find(book, 'firstname').then(console.log)
// [
// { path: 'author.firstname', data: { value: 'Dianne' } },
// { path: 'editor.firstname', data: { value: 'Felicity' } }
// ]

hays.find(book, /.*name/).then(console.log)
// [
// { path: 'author.lastname', data: { value: 'Windler' } },
// { path: 'author.firstname', data: { value: 'Dianne' } },
// { path: 'editor.lastname', data: { value: 'Smith' } },
// { path: 'editor.firstname', data: { value: 'Felicity' } }
// ]

hays.find(book, 'title', { transform: s => s.toLowerCase() }).then(console.log)
// [
// {
// path: 'title',
// data: {
// value: 'The Ultimate Database Study Guide',
// transformed: 'the ultimate database study guide',
// },
// }
// ]

hays.replace(book, 'category', c => c.concat('Educational')).then(console.log)
// [
// {
// path: 'category',
// data: {
// old: [ 'Non-Fiction', 'Technology' ],
// new: [ 'Non-Fiction', 'Technology', 'Educational' ]
// }
// }
// ]

console.log(book)
// {
// isbn: '123-456-222',
// title: 'The Ultimate Database Study Guide',
// category: ['Non-Fiction', 'Technology', 'Educational'],
// author: { lastname: 'Windler', firstname: 'Dianne' },
// editor: { lastname: 'Smith', firstname: 'Felicity' },
// }
```