Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
env: {
es2023: true,
node: true,
mocha: true
},
extends: ['semistandard', 'eslint:recommended'],
rules: {
complexity: ['error', 13] // eventually get down to 10 or less
}
};
23 changes: 23 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

name: Node.js CI
on: push
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- name: Run tests
run: npx nyc@latest --reporter=lcov npm test
- uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/lib-cov/
/node_modules/
/npm-debug.log
.idea
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock=false
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
3 changes: 0 additions & 3 deletions .travis.yml

This file was deleted.

56 changes: 26 additions & 30 deletions lib/dotaccess.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,40 @@
module.exports.set = set
module.exports.unset = unset
module.exports.get = get
module.exports.set = set;
module.exports.unset = unset;
module.exports.get = get;

function parts(key) {
if (Array.isArray(key)) return key
return key.split('.')
function parts (key) {
if (Array.isArray(key)) return key;
return key.split('.');
}

function lookup(obj, key) {
key = parts(key)
var lastKey = key.pop()
for (var i=0, l=key.length; i<l; i++) {
var part = key[i]
if (!(part in obj)) obj[part] = {}
obj = obj[part]
if (!obj) throw new Error('dotaccess: incompatible value in ' + part)
function lookup (obj, key) {
key = parts(key);
const lastKey = key.pop();
for (let i = 0, l = key.length; i < l; i++) {
const part = key[i];
if (!(part in obj)) obj[part] = {};
obj = obj[part];
if (!obj) throw new Error('dotaccess: incompatible value in ' + part);
}
return [obj, lastKey];
}

function set(obj, key, value, overwrite) {
var objectAndKey = lookup(obj, key)
, obj = objectAndKey[0]
, key = objectAndKey[1];
if (overwrite || !(key in obj)) obj[key] = value
function set (obj, key, value, overwrite) {
[obj, key] = lookup(obj, key);
if (overwrite || !(key in obj)) obj[key] = value;
}

function unset(obj, key) {
var objectAndKey = lookup(obj, key)
, obj = objectAndKey[0]
, key = objectAndKey[1];
function unset (obj, key) {
[obj, key] = lookup(obj, key);
return delete obj[key];
}

function get(obj, key, def) {
key = parts(key)
for (var i=0, l=key.length; i<l; i++) {
var part = key[i]
if (!(part in obj)) return def
obj = obj[part]
function get (obj, key, def) {
key = parts(key);
for (let i = 0, l = key.length; i < l; i++) {
const part = key[i];
if (!(part in obj)) return def;
obj = obj[part];
}
return obj
return obj;
}
17 changes: 13 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@
"type": "git",
"url": "https://github.com/daaku/nodejs-dotaccess"
},
"scripts": { "test": "NODE_PATH=./lib mocha --ui exports" },
"devDependencies": {
"mocha": "0.12.x"
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"test": "NODE_PATH=./lib mocha --ui exports"
},
"engines": { "node": "0.6.x" }
"devDependencies": {
"eslint": "^8.57.1",
"eslint-config-semistandard": "^17.0.0",
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-n": "^15.7.0",
"eslint-plugin-promise": "^6.6.0",
"mocha": "^11.5.0"
}
}
128 changes: 64 additions & 64 deletions test/dotaccess.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,101 @@
var dotaccess = require('dotaccess')
, assert = require('assert')
const dotaccess = require('dotaccess');
const assert = require('assert');

exports['get stuff'] = function() {
var obj = {
exports['get stuff'] = function () {
const obj = {
answer: 42,
deep: {
trench: 43,
'funky names': 44,
'including a .': 45
}
}

assert.equal(42, dotaccess.get(obj, 'answer'), 'Expect 42 - dot string')
assert.equal(42, dotaccess.get(obj, ['answer']), 'Expect 42 - array')
assert.equal(43, dotaccess.get(obj, 'deep.trench'), 'Expect 43 - dot string')
assert.equal(43, dotaccess.get(obj, ['deep', 'trench']), 'Expect 43 - array')
assert.equal(44, dotaccess.get(obj, 'deep.funky names'), 'Expect 44 - dot string')
assert.equal(45, dotaccess.get(obj, ['deep', 'including a .']), 'Expect 45 - array')
assert.equal(46, dotaccess.get(obj, 'missing', 46), 'Expect 46')
assert.equal(46, dotaccess.get(obj, ['missing'], 46), 'Expect 46')
}

exports['unset stuff'] = function() {
var obj = {
};

assert.equal(42, dotaccess.get(obj, 'answer'), 'Expect 42 - dot string');
assert.equal(42, dotaccess.get(obj, ['answer']), 'Expect 42 - array');
assert.equal(43, dotaccess.get(obj, 'deep.trench'), 'Expect 43 - dot string');
assert.equal(43, dotaccess.get(obj, ['deep', 'trench']), 'Expect 43 - array');
assert.equal(44, dotaccess.get(obj, 'deep.funky names'), 'Expect 44 - dot string');
assert.equal(45, dotaccess.get(obj, ['deep', 'including a .']), 'Expect 45 - array');
assert.equal(46, dotaccess.get(obj, 'missing', 46), 'Expect 46');
assert.equal(46, dotaccess.get(obj, ['missing'], 46), 'Expect 46');
};

exports['unset stuff'] = function () {
const obj = {
answer: 42,
deep: {
trench: 43,
'funky names': 44,
'including a .': 45
}
}
};

assert(dotaccess.unset(obj, 'answer'))
assert.equal(undefined, dotaccess.get(obj, 'answer'), 'Expect unset - string')
assert(dotaccess.unset(obj, 'answer'));
assert.equal(undefined, dotaccess.get(obj, 'answer'), 'Expect unset - string');

assert(dotaccess.unset(obj, ['answer']))
assert.equal(undefined, dotaccess.get(obj, 'answer'), 'Expect unset - single element array')
assert(dotaccess.unset(obj, ['answer']));
assert.equal(undefined, dotaccess.get(obj, 'answer'), 'Expect unset - single element array');

assert(dotaccess.unset(obj, 'deep.trench'))
assert.equal(undefined, dotaccess.get(obj, 'deep.trench'), 'Expect unset - dot string')
assert(dotaccess.unset(obj, 'deep.trench'));
assert.equal(undefined, dotaccess.get(obj, 'deep.trench'), 'Expect unset - dot string');

assert(dotaccess.unset(obj, ['deep', 'trench']))
assert.equal(undefined, dotaccess.get(obj, ['deep', 'trench']), 'Expect unset - array')
assert(dotaccess.unset(obj, ['deep', 'trench']));
assert.equal(undefined, dotaccess.get(obj, ['deep', 'trench']), 'Expect unset - array');

assert(dotaccess.unset(obj, 'deep.funky names'))
assert.equal(undefined, dotaccess.get(obj, 'deep.funky names'), 'Expect unset - dot string funky names')
assert(dotaccess.unset(obj, 'deep.funky names'));
assert.equal(undefined, dotaccess.get(obj, 'deep.funky names'), 'Expect unset - dot string funky names');

assert(dotaccess.unset(obj, ['deep', 'including a .']))
assert.equal(undefined, dotaccess.get(obj, ['deep', 'including a .']), 'Expect unset - array including dot')
assert(dotaccess.unset(obj, ['deep', 'including a .']));
assert.equal(undefined, dotaccess.get(obj, ['deep', 'including a .']), 'Expect unset - array including dot');

assert(dotaccess.unset(obj, 'missing'))
assert.equal(undefined, dotaccess.get(obj, 'missing'), 'Expect unset - missing')
assert(dotaccess.unset(obj, 'missing'));
assert.equal(undefined, dotaccess.get(obj, 'missing'), 'Expect unset - missing');

assert(dotaccess.unset(obj, ['missing']))
assert.equal(undefined, dotaccess.get(obj, ['missing']), 'Expect unset - missing')
}
assert(dotaccess.unset(obj, ['missing']));
assert.equal(undefined, dotaccess.get(obj, ['missing']), 'Expect unset - missing');
};

exports['set stuff'] = function() {
var obj = {
exports['set stuff'] = function () {
const obj = {
existing: {
one: 47
}
}

dotaccess.set(obj, 'answer', 42)
dotaccess.set(obj, 'deep.trench', 43)
dotaccess.set(obj, ['deep', 'funky names'], 44)
dotaccess.set(obj, ['deep', 'including a .'], 45)
dotaccess.set(obj, 'existing.one', 'foo bared')

assert.equal(42, dotaccess.get(obj, 'answer'), 'Expect 42 - dot string')
assert.equal(42, dotaccess.get(obj, ['answer']), 'Expect 42 - array')
assert.equal(43, dotaccess.get(obj, 'deep.trench'), 'Expect 43 - dot string')
assert.equal(43, dotaccess.get(obj, ['deep', 'trench']), 'Expect 43 - array')
assert.equal(44, dotaccess.get(obj, 'deep.funky names'), 'Expect 44 - dot string')
assert.equal(45, dotaccess.get(obj, ['deep', 'including a .']), 'Expect 45 - array')
assert.equal(46, dotaccess.get(obj, 'missing', 46), 'Expect 46')
assert.equal(46, dotaccess.get(obj, ['missing'], 46), 'Expect 46')
assert.equal(47, dotaccess.get(obj, ['existing', 'one']), 'Expect 47 - array')
}

exports['failed sets'] = function() {
var obj = {
};

dotaccess.set(obj, 'answer', 42);
dotaccess.set(obj, 'deep.trench', 43);
dotaccess.set(obj, ['deep', 'funky names'], 44);
dotaccess.set(obj, ['deep', 'including a .'], 45);
dotaccess.set(obj, 'existing.one', 'foo bared');

assert.equal(42, dotaccess.get(obj, 'answer'), 'Expect 42 - dot string');
assert.equal(42, dotaccess.get(obj, ['answer']), 'Expect 42 - array');
assert.equal(43, dotaccess.get(obj, 'deep.trench'), 'Expect 43 - dot string');
assert.equal(43, dotaccess.get(obj, ['deep', 'trench']), 'Expect 43 - array');
assert.equal(44, dotaccess.get(obj, 'deep.funky names'), 'Expect 44 - dot string');
assert.equal(45, dotaccess.get(obj, ['deep', 'including a .']), 'Expect 45 - array');
assert.equal(46, dotaccess.get(obj, 'missing', 46), 'Expect 46');
assert.equal(46, dotaccess.get(obj, ['missing'], 46), 'Expect 46');
assert.equal(47, dotaccess.get(obj, ['existing', 'one']), 'Expect 47 - array');
};

exports['failed sets'] = function () {
const obj = {
bool: true,
integer: 42,
floating: 0.5
}
};

assert.throws(function() {
assert.throws(function () {
dotaccess.set(obj, 'bool.one', 42);
}, 'cant hang off a boolean');

assert.throws(function() {
assert.throws(function () {
dotaccess.set(obj, 'integer.one', 42);
}, 'cant hang off a integer');

assert.throws(function() {
assert.throws(function () {
dotaccess.set(obj, 'floating.one', 42);
}, 'cant hang off a float');
}
};