1.1.0 • Published 3 years ago

propx v1.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
3 years ago

Safely accessing deeply nested values ⚡️

For deno.land

import { get, set } from "https://raw.githubusercontent.com/LuisPaGarcia/propx/master/mod.ts";

var obj = {
  child:{
    value: 1
  }
}

// get
console.log( get(obj, 'child.value') ) // 1
console.log( get(obj, 'child.value.nonexist') ) // undefined
console.log( get(obj, 'child.value.nonexist', "10") ) // 10

// set
console.log( set(obj, 'child.otherValue', ['hey']) )
// -> 
// {
//   child:{
//     value: 1,
//     otherValue: ['hey']
//   }
// }

For Node.js

  1. Install it using npm:
$ npm install propx 

or yarn:

$ yarn add propx 

Usage

// Using require
const { set, get } = require('propx');

// Using import (es6)
import { set, get } from 'propx';

// Target example
const obj = {
    a: 1,
    b: 2,
    c: { d: { e: { f: 3 } } },
    g: undefined
};

// How to use get
console.log(get(obj, 'c.d.e.f', ''))
//-> 3

// How to use set
console.log(set(obj, 'g.x.y.z', ['hey']))
/* -> {
    a: 1,
    b: 2,
    c: { d: { e: { f: 3 } } },
    g: { x: { y: { z: [ 'hey' ] } } }
}

*/

Definition

/**
 * @param {object} object base object to inject new prop
 * @param {string} path path to set the object
 * @param {any} value value to set
 */
function set(object: Object, path: string, value: any): Object {
    ...
}

/**
 *
 * @param {object} object object to access
 * @param { string | string[] } key string or array with dot notation path
 * @param { any } def optional parameter for default if the full key in path is missing
 */
function get(object: Object, key: string | string[], def: any) {
...
}

TODO:

  • Write tests
  • Use microbundle to generate module units.
  • Configure umd ussage for unpkg.com.
1.1.0

3 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago