-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creates jsdig based on ruby's hash#dig
- Loading branch information
0 parents
commit 4de4b21
Showing
8 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@babel/preset-env" | ||
], | ||
[ | ||
"minify" | ||
] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module.exports = { | ||
env: { | ||
browser: true, | ||
es6: true, | ||
}, | ||
extends: [ | ||
'airbnb-base', | ||
], | ||
globals: { | ||
Atomics: 'readonly', | ||
SharedArrayBuffer: 'readonly', | ||
}, | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
sourceType: 'module', | ||
}, | ||
rules: { | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# nyc test coverage | ||
.nyc_output | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directories | ||
node_modules | ||
jspm_packages | ||
|
||
# Optional npm cache directory | ||
.npm | ||
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
# Editors | ||
.idea | ||
|
||
# Lib | ||
lib | ||
|
||
# npm package lock | ||
package-lock.json | ||
yarn.lock | ||
|
||
others | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2020 Christoph Drechsler <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# License | ||
|
||
MIT © Christoph Drechsler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "jsdig", | ||
"version": "0.0.1", | ||
"description": "Based on Ruby's hash#dig", | ||
"main": "./lib/index.js", | ||
"scripts": { | ||
"test": "mocha --require @babel/register", | ||
"test:prod": "cross-env BABEL_ENV=production npm run test", | ||
"build": "BABEL_ENV=production babel src --out-dir lib", | ||
"prepublish": "npm run test && npm run build" | ||
}, | ||
"files": [ | ||
"lib", | ||
"src" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/devchris/jsdig.git" | ||
}, | ||
"keywords": [ | ||
"javascript", | ||
"ruby", | ||
"dig", | ||
"hash" | ||
], | ||
"author": "Christoph Drechsler <[email protected]>", | ||
"license": "MIT", | ||
"homepage": "https://github.com/devchris/jsdig#readme", | ||
"devDependencies": { | ||
"@babel/cli": "^7.8.4", | ||
"@babel/core": "^7.9.0", | ||
"@babel/preset-env": "^7.9.5", | ||
"@babel/register": "^7.9.0", | ||
"babel-plugin-add-module-exports": "^1.0.0", | ||
"babel-polyfill": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"babel-preset-minify": "^0.5.0", | ||
"chai": "^4.2.0", | ||
"mocha": "^7.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const Dig = (obj, ...keys) => { | ||
if (typeof obj !== 'object' || obj === null) return null; | ||
|
||
let digest = obj; | ||
|
||
for (let i = 0, len = keys.length; i < len; i++) { | ||
if (typeof digest === 'undefined' || digest === null) { | ||
return null; | ||
} | ||
|
||
digest = digest[keys[i]]; | ||
} | ||
|
||
return digest; | ||
}; | ||
|
||
export default Dig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { assert } from 'chai'; | ||
import Dig from '../src'; | ||
|
||
describe('Dig', () => { | ||
it('returns null if passed in value is not an object or array', () => { | ||
assert(Dig('', 'name') === null); | ||
}); | ||
|
||
it('returns the wanted value from an object', () => { | ||
const testObject = { name: 'Christoph', label: 'C' }; | ||
|
||
assert(Dig(testObject, 'name') === 'Christoph'); | ||
}) | ||
|
||
it('returns the wanted value from a nested object', () => { | ||
const testObject = { | ||
name: 'Christoph', | ||
labels: { | ||
first: { | ||
value: '123' | ||
} | ||
} | ||
}; | ||
|
||
assert(Dig(testObject, 'labels', 'first', 'value') === '123'); | ||
}) | ||
|
||
it('returns the wanted value from a nested object inside an array', () => { | ||
const testObject = [{ | ||
name: 'Christoph', | ||
labels: { | ||
first: { | ||
value: '123' | ||
} | ||
} | ||
}]; | ||
|
||
assert(Dig(testObject, 0, 'labels', 'first', 'value') === '123'); | ||
}) | ||
|
||
it('returns the wanted value from a nested object inside an array of an object', () => { | ||
const testObject = { | ||
other: { | ||
more: [{ | ||
name: 'Christoph', | ||
labels: { | ||
first: { | ||
value: '123' | ||
} | ||
} | ||
}] | ||
} | ||
}; | ||
|
||
assert(Dig(testObject, 'other', 'more', 0, 'labels', 'first', 'value') === '123'); | ||
}) | ||
|
||
it('will return null if key does not exist', () => { | ||
const testObject = { | ||
other: { | ||
more: [{ | ||
name: 'Christoph', | ||
labels: { | ||
first: { | ||
value: '123' | ||
} | ||
} | ||
}] | ||
} | ||
}; | ||
|
||
assert(Dig(testObject, 'other', 'more', 0, 'wrong', 'first', 'value') === null); | ||
}) | ||
}); |