Skip to content

Commit

Permalink
Creates jsdig based on ruby's hash#dig
Browse files Browse the repository at this point in the history
  • Loading branch information
devchris committed Apr 10, 2020
0 parents commit 4de4b21
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
[
"@babel/preset-env"
],
[
"minify"
]
]
}
19 changes: 19 additions & 0 deletions .eslintrc.js
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: {
},
};
44 changes: 44 additions & 0 deletions .gitignore
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
21 changes: 21 additions & 0 deletions LICENSE
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.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# License

MIT © Christoph Drechsler
41 changes: 41 additions & 0 deletions package.json
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"
}
}
17 changes: 17 additions & 0 deletions src/index.js
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;
74 changes: 74 additions & 0 deletions test/index.js
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);
})
});

0 comments on commit 4de4b21

Please sign in to comment.