Skip to content

Commit 2f33a81

Browse files
committed
Improve tests & readme, add travis-ci, ES Modules support & rollup to transpile to commonjs
1 parent f98e516 commit 2f33a81

File tree

6 files changed

+79
-11
lines changed

6 files changed

+79
-11
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/dist.js
2+
/node_modules
3+
/npm-debug.log

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: node_js
2+
node_js:
3+
- 6

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# `dlv(obj, keypath)` [![NPM](https://img.shields.io/npm/v/dlv.svg)](https://npmjs.com/package/dlv) [![Build](https://travis-ci.org/developit/dlv.svg?branch=master)](https://travis-ci.org/developit/dlv)
2+
3+
> Safely get a dot-notated path within a nested object.
4+
5+
### Why?
6+
7+
Smallest possible implementation: only **120 bytes.**
8+
9+
You could write this yourself, but then you'd have to write [tests].
10+
11+
Implementation is ripped directly from [preact].
12+
13+
Supports ES Modules, CommonJS and globals.
14+
15+
### Installation
16+
17+
`npm install --save dlv`
18+
19+
20+
### Usage
21+
22+
```js
23+
import delve from 'dlv';
24+
25+
let obj = {
26+
a: {
27+
b: {
28+
c: 1
29+
}
30+
}
31+
};
32+
33+
delve(obj, 'a.b.c') === 1;
34+
35+
delve(obj, 'a.b') === obj.a.b;
36+
37+
delve(obj, 'a.b.c.d') === undefined;
38+
```
39+
40+
### License
41+
42+
MIT
43+
44+
45+
[preact]: https://github.com/developit/preact
46+
[tests]: https://github.com/developit/dlv/blob/master/test.js

index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
if (typeof module!=='undefined') module.exports = dlv;
2-
3-
function dlv(obj, key) {
1+
export default function dlv(obj, key) {
42
if (key.split) key = key.split('.');
53
for (var i=0; i<key.length && obj; i++) obj = obj[key[i]];
64
return obj;
75
}
8-

package.json

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,26 @@
22
"name": "dlv",
33
"version": "1.0.0",
44
"description": "Safely get a dot-notated property within an object.",
5-
"main": "index.js",
5+
"main": "dist.js",
6+
"jsnext:main": "index.js",
67
"scripts": {
8+
"build": "rollup -f cjs --no-strict $npm_package_jsnext_main | uglifyjs -cm -o $npm_package_main",
9+
"prepublish": "npm run build",
710
"test": "node test"
811
},
9-
"keywords": ["delve","dot notation","dot"],
10-
"files": ["index.js"],
12+
"keywords": [
13+
"delve",
14+
"dot notation",
15+
"dot"
16+
],
17+
"files": [
18+
"index.js",
19+
"dist.js"
20+
],
1121
"author": "Jason Miller <[email protected]>",
12-
"license": "MIT"
22+
"license": "MIT",
23+
"devDependencies": {
24+
"rollup": "^0.34.3",
25+
"uglifyjs": "^2.4.10"
26+
}
1327
}

test.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var assert = require('assert');
2-
var dlv = require('.');
2+
var delve = require('.');
33

44
var obj = {
55
one: 1,
@@ -14,9 +14,14 @@ var obj = {
1414
}
1515
};
1616

17+
// assert equality of a given path, as dot notation and array.
1718
function check(path, value) {
18-
assert.equal(dlv(obj, path), value);
19-
console.log(' ✓ dlv(obj, "'+path+'")');
19+
assert.equal(delve(obj, path), value);
20+
console.log(' ✓ delve(obj, "'+path+'")');
21+
22+
var arr = path.split('.');
23+
assert.equal(delve(obj, arr), value);
24+
console.log(' ✓ delve(obj, '+JSON.stringify(arr)+')');
2025
}
2126

2227
check('', undefined);

0 commit comments

Comments
 (0)