[Pkg-javascript-commits] [node-delve] 01/02: Imported Upstream version 0.3.2
Thorsten Alteholz
alteholz at moszumanska.debian.org
Sun Mar 27 15:32:22 UTC 2016
This is an automated email from the git hooks/post-receive script.
alteholz pushed a commit to branch master
in repository node-delve.
commit 3f6e3b5fdb705f553d393197ed8d939e973d9cb2
Author: Thorsten Alteholz <debian at alteholz.de>
Date: Sun Mar 27 17:32:19 2016 +0200
Imported Upstream version 0.3.2
---
README.markdown | 58 ++++++++++++++++++++++++++++++++++++++++++
package.json | 44 ++++++++++++++++++++++++++++++++
src/delve.js | 40 +++++++++++++++++++++++++++++
test/delve-test.js | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 216 insertions(+)
diff --git a/README.markdown b/README.markdown
new file mode 100644
index 0000000..0f13cc5
--- /dev/null
+++ b/README.markdown
@@ -0,0 +1,58 @@
+# delve
+Delve recursively into a value to retrieve a property; without erroring.
+
+[![browser support](https://ci.testling.com/hughfdjackson/delve.png)](http://ci.testling.com/hughfdjackson/delve)
+
+## Why
+It sucks to have to do `if ( obj && obj.prop && obj.prop.secondProp ) { ... }`.
+
+## Example
+
+### delve
+
+```javascript
+var delve = require('delve')
+
+var o = { x: { y: { z: 'my val' } } }
+
+delve(o, 'x.y') //= { z: 'my val' }
+delve(o, 'x.y.z') //= 'my val'
+delve(o, 'x.y.z.foo') //= undefined
+delve(undefined, 'x.y.z.foo') //= undefined
+delve(null, 'x.y.z.foo') //= undefined
+delve('foo', 'length') //= 3
+```
+### delve.has
+
+```javascript
+var delve = require('delve')
+
+delve.has({ x: { y: undefined } }, 'x.y') //= true
+delve.has('foo', 'length') //= true
+delve.has(null, 'foo') //= false
+delve.has({ x: { } }, 'x.y') //= false
+```
+
+For more examples, see tests/delve-test.js.
+
+## Install
+
+### npm
+
+```bash
+npm install delve
+```
+
+### browser
+
+Download src/delve.js, and include it as a script tag.
+
+### AMD/require.js
+
+Download src/delve.js, and require it in:
+
+```javascript
+require(['libs/delve'], function(delve){
+ // ... assuming delve is in libs/delve, now it's ready to use
+})
+```
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..2876527
--- /dev/null
+++ b/package.json
@@ -0,0 +1,44 @@
+{
+ "name": "delve",
+ "version": "0.3.2",
+ "main": "src/delve.js",
+ "scripts": {
+ "test": "./node_modules/mocha/bin/mocha --ui qunit",
+ "prepublish": "npm test"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/hughfdjackson/delve.git"
+ },
+ "testling": {
+ "files": "test/*-test.js",
+ "browsers": [
+ "iexplore/6.0",
+ "iexplore/7.0",
+ "iexplore/8.0",
+ "iexplore/9.0",
+ "iexplore/10.0",
+ "chrome/4.0",
+ "chrome/23.0",
+ "firefox/3.0",
+ "firefox/17.0",
+ "opera/10.0",
+ "opera/12.0",
+ "safari/5.0.5",
+ "safari/5.1"
+ ],
+ "harness": "mocha-qunit"
+ },
+ "devDependencies": {
+ "es5-shim": "2.0.5",
+ "mocha": "1.8.1"
+ },
+ "keywords": [
+ "property",
+ "accessor",
+ "utility",
+ "sugar"
+ ],
+ "author": "hughfdjackson",
+ "license": "BSD"
+}
diff --git a/src/delve.js b/src/delve.js
new file mode 100644
index 0000000..22b89c9
--- /dev/null
+++ b/src/delve.js
@@ -0,0 +1,40 @@
+void function(root){
+
+ 'use strict'
+
+ var isObject = function(v){ return v !== null && typeof v === 'object' }
+
+ var unpackOne = function(o, path){
+ if ( o == null ) return
+ else return o[path]
+ }
+
+ var delve = function(o, path){
+ var parts = path.split('.')
+ var result = o
+ var part
+
+ while ( parts.length ) {
+ part = parts.shift()
+ result = unpackOne(result, part)
+ }
+
+ return result
+ }
+
+ delve.has = function(o, path){
+ if ( delve(o, path) !== undefined ) return true
+
+ var parts = path.split('.')
+ var unpacked = delve(o, parts.slice(0, -1).join('.'))
+ var last = parts.slice(-1)
+
+ if ( isObject(unpacked) ) return last in unpacked
+ else return false
+ }
+
+ if ( typeof module !== 'undefined' && delve(module, 'exports') ) module.exports = delve
+ else if ( typeof define === 'function' && delve(define, 'amd') ) define(function(){ return delve })
+ else root.delve = delve
+
+}(this)
diff --git a/test/delve-test.js b/test/delve-test.js
new file mode 100644
index 0000000..98d2478
--- /dev/null
+++ b/test/delve-test.js
@@ -0,0 +1,74 @@
+var a = require('assert')
+var delve = require('..')
+
+// to test older browsers with es5-shimmed functionality
+require('es5-shim')
+
+
+suite('delve')
+
+test('present property', function(){
+ var o = { x: { y: { z: 3 } } }
+ a.equal(delve(o, 'x.y.z'), 3)
+})
+
+test('short path', function(){
+ var o = { x: 3 }
+ a.equal(delve(o, 'x'), 3)
+})
+
+test('prop of a non-null, non-undefined primitive', function(){
+ a.equal(delve('foo', 'length'), 3)
+ a.equal(delve(3, 'toString'), Number.prototype.toString)
+ a.equal(delve(true, 'toString'), Boolean.prototype.toString)
+});
+
+test('undefined as first argument', function(){
+ a.equal(delve(undefined, 'x.y.z'), undefined)
+})
+
+test('missing prop', function(){
+ var o = { x: { b: { c: 3 } } }
+ a.equal(delve(o, 'x.y.z'), undefined)
+})
+
+test('empty string', function(){
+ var o = { x: { y: { x: 3 } } }
+ a.equal(delve(o, ''), undefined)
+})
+
+test('return is object', function(){
+ var inner = { x: 3 }
+ var o = { x: { y: inner } }
+
+ a.equal(delve(o, 'x.y'), inner)
+})
+
+test('delve into an array', function(){
+ var o = [1, { x: [{ y: [{ z: 3 }] }] }]
+
+ a.equal(delve(o, '1.x.0.y.0.z'), 3)
+})
+
+test('primitive value part way through', function(){
+ var cases = [
+ { x: { y: 'string' } },
+ { x: { y: 1 } },
+ { x: { y: null } },
+ { x: { y: false } }
+ ]
+
+ cases.forEach(function(o){
+ a.equal(delve(o, 'x.y.z'), undefined)
+ a.equal(delve.has(o, 'x.y.z'), false)
+ })
+})
+
+test('delve.has on undefined as a value', function(){
+ var o = { x: { y: { z: undefined } } }
+ a.equal(delve.has(o, 'x.y.z'), true)
+})
+
+test('delve.has on a string', function(){
+ a.equal(delve.has('foo', 'length'), true)
+})
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-delve.git
More information about the Pkg-javascript-commits
mailing list