Comparing version 1.0.2 to 1.1.0
28
index.js
@@ -12,9 +12,11 @@ 'use strict'; | ||
*/ | ||
set: function(obj, key, val) { | ||
let k = key; | ||
if(k.indexOf('.') > -1) { | ||
let segments = key.split('.'); | ||
k = segments.pop(); | ||
set: function(obj, parts, val) { | ||
if(!Array.isArray(parts)) { | ||
parts = parts.split('.'); | ||
} | ||
let k = parts[0]; | ||
if(parts.length > 1) { | ||
k = parts.pop(); | ||
segments.forEach(function(k) { | ||
parts.forEach(function(k) { | ||
if(! obj.hasOwnProperty(k)) { | ||
@@ -35,9 +37,11 @@ obj[k] = {}; | ||
*/ | ||
get: function(obj, key, def) { | ||
let k = key; | ||
if(obj && k.indexOf('.') > -1) { | ||
let segments = key.split('.'); | ||
k = segments.pop(); | ||
get: function(obj, parts, def) { | ||
if(!Array.isArray(parts)) { | ||
parts = parts.split('.'); | ||
} | ||
let k = parts[0]; | ||
if(parts.length > -1) { | ||
k = parts.pop(); | ||
segments.forEach(function(k) { | ||
parts.forEach(function(k) { | ||
if(! obj.hasOwnProperty(k)) { | ||
@@ -44,0 +48,0 @@ obj = false; |
{ | ||
"name": "dot2val", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Set or get a value within a deeply nested object using `dot' notation", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha", | ||
"test-travis": "istanbul cover _mocha && codeclimate-test-reporter < coverage/lcov.info" | ||
}, | ||
@@ -17,5 +18,6 @@ "repository": { | ||
"object", | ||
"dot" | ||
"dot", | ||
"pathval" | ||
], | ||
"author": "Brook Yang", | ||
"author": "Brook Yang <uedsky@gmail.com>", | ||
"license": "MIT", | ||
@@ -22,0 +24,0 @@ "bugs": { |
# dot2val | ||
[![Build Status](https://travis-ci.org/yangg/dot2val.svg?branch=master)](https://travis-ci.org/yangg/dot2val) [![Code Climate](https://codeclimate.com/github/yangg/dot2val/badges/gpa.svg)](https://codeclimate.com/github/yangg/dot2val) [![npm:](https://img.shields.io/npm/v/dot2val.svg?style=flat)](https://www.npmjs.com/packages/dot2val) | ||
[![Build Status](https://travis-ci.org/yangg/dot2val.svg?branch=master)](https://travis-ci.org/yangg/dot2val) [![Test Coverage](https://codeclimate.com/github/yangg/dot2val/badges/coverage.svg)](https://codeclimate.com/github/yangg/dot2val/coverage) [![Code Climate](https://codeclimate.com/github/yangg/dot2val/badges/gpa.svg)](https://codeclimate.com/github/yangg/dot2val) [![npm:](https://img.shields.io/npm/v/dot2val.svg?style=flat)](https://www.npmjs.com/packages/dot2val) | ||
@@ -9,9 +9,9 @@ Set or get a value within a deeply nested object using `dot' notation | ||
```bash | ||
npm install dot2val | ||
npm install --save dot2val | ||
``` | ||
## Usage | ||
```js | ||
var dot2val = require('dot2val'); | ||
``` | ||
var props = require('dot2val'); | ||
``` | ||
@@ -37,18 +37,14 @@ Given: | ||
<!-- js | ||
var props = require('./'); | ||
--> | ||
#### get | ||
```js | ||
props.get(obj, 'prop1.str'); // => "Hello" | ||
props.get(obj, 'prop1.arr.2'); // => "c" | ||
props.get(obj, 'prop2.arr.0.nested'); // => "Universe" | ||
dot2val.get(obj, 'prop1.str'); // => "Hello" | ||
dot2val.get(obj, 'prop1.arr.2'); // => "c" | ||
dot2val.get(obj, 'prop2.arr.0.nested'); // => "Universe" | ||
props.get(arr, '0.foo'); // => "bar" | ||
dot2val.get(arr, '0.foo'); // => "bar" | ||
props.get(undefined, 'doesnt.matter'); // => undefined | ||
props.get({}, 'doesnt.exist'); // => undefined | ||
props.get({}, 'doesnt.exist', 'default value'); // => "default value" | ||
dot2val.get(undefined, 'doesnt.matter'); // => undefined | ||
dot2val.get({}, 'doesnt.exist'); // => undefined | ||
dot2val.get({}, 'doesnt.exist', 'default value'); // => "default value" | ||
``` | ||
@@ -59,7 +55,10 @@ | ||
```js | ||
props.set(obj, 'hello.universe', 'properties'); | ||
props.set(obj, 'hello', 'universe'); | ||
dot2val.set(obj, 'hello.universe', 'properties'); | ||
dot2val.set(obj, 'hello1', 'universe'); | ||
dot2val.set(obj, 'prop2'); // delete obj['prop2'] | ||
``` | ||
[Try dot2val in your browser](https://tonicdev.com/yangg/dot2val) | ||
## License | ||
MIT |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
7054
89
62