-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
124 additions
and
122 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
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
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,2 @@ | ||
singleQuote: true | ||
semi: false |
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
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 |
---|---|---|
@@ -1,54 +1,54 @@ | ||
class Notes { | ||
constructor(notes) { | ||
if (notes && typeof notes === "object") { | ||
Object.assign(this, notes); | ||
constructor(notes) { | ||
if (notes && typeof notes === 'object') { | ||
Object.assign(this, notes) | ||
} | ||
|
||
Object.defineProperty(this, 'set', { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: assignPathValue.bind(this), | ||
}) | ||
|
||
Object.defineProperty(this, 'get', { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: getPathValue.bind(this), | ||
}) | ||
} | ||
|
||
Object.defineProperty(this, "set", { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: assignPathValue.bind(this), | ||
}); | ||
|
||
Object.defineProperty(this, "get", { | ||
configurable: false, | ||
enumerable: false, | ||
writable: false, | ||
value: getPathValue.bind(this), | ||
}); | ||
} | ||
} | ||
|
||
module.exports = Notes; | ||
module.exports = Notes | ||
|
||
function getSegments(path) { | ||
// a dot.delimited.path | ||
if (typeof path === "string") return path.split("."); | ||
// a dot.delimited.path | ||
if (typeof path === 'string') return path.split('.') | ||
|
||
// ['one', 'two', 'thr.ee'] | ||
if (Array.isArray(path)) return path; | ||
// ['one', 'two', 'thr.ee'] | ||
if (Array.isArray(path)) return path | ||
} | ||
|
||
function assignPathValue(path, value) { | ||
if (path === undefined || value === undefined) return; | ||
if (path === undefined || value === undefined) return | ||
|
||
const segments = getSegments(path); | ||
let dest = this; | ||
const segments = getSegments(path) | ||
let dest = this | ||
|
||
while (segments.length > 1) { | ||
while (segments.length > 1) { | ||
// create any missing paths | ||
if (!dest[segments[0]]) dest[segments[0]] = {}; | ||
// set dest one path segment deeper | ||
dest = dest[segments.shift()]; | ||
} | ||
dest[segments[0]] = value; | ||
if (!dest[segments[0]]) dest[segments[0]] = {} | ||
// set dest one path segment deeper | ||
dest = dest[segments.shift()] | ||
} | ||
dest[segments[0]] = value | ||
} | ||
|
||
function getPathValue(path) { | ||
if (!path) return; | ||
const segments = getSegments(path); | ||
return segments.reduce((prev, curr) => { | ||
return prev ? prev[curr] : undefined; | ||
}, this); | ||
if (!path) return | ||
const segments = getSegments(path) | ||
return segments.reduce((prev, curr) => { | ||
return prev ? prev[curr] : undefined | ||
}, this) | ||
} |
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 |
---|---|---|
@@ -1,93 +1,93 @@ | ||
const assert = require("assert"); | ||
const assert = require('assert') | ||
|
||
const Notes = require("../index"); | ||
const Notes = require('../index') | ||
|
||
describe("notes", () => { | ||
beforeEach((done) => { | ||
this.notes = new Notes(); | ||
done(); | ||
}); | ||
describe('notes', () => { | ||
beforeEach((done) => { | ||
this.notes = new Notes() | ||
done() | ||
}) | ||
|
||
it("exports an object", (done) => { | ||
it('exports an object', (done) => { | ||
// console.log(this.notes) | ||
assert.ok(typeof this.notes === "object"); | ||
done(); | ||
}); | ||
assert.ok(typeof this.notes === 'object') | ||
done() | ||
}) | ||
|
||
const functionList = ["get", "set"]; | ||
const functionList = ['get', 'set'] | ||
|
||
functionList.forEach((fn) => { | ||
it(`has ${fn}()`, (done) => { | ||
assert.equal(typeof this.notes[fn], "function"); | ||
done(); | ||
}); | ||
}); | ||
functionList.forEach((fn) => { | ||
it(`has ${fn}()`, (done) => { | ||
assert.equal(typeof this.notes[fn], 'function') | ||
done() | ||
}) | ||
}) | ||
|
||
functionList.forEach((fn) => { | ||
it(`ignores attempts to redefine ${fn}`, (done) => { | ||
this.notes[fn] = "turd"; | ||
this.notes[fn]("turd"); | ||
done(); | ||
}); | ||
}); | ||
functionList.forEach((fn) => { | ||
it(`ignores attempts to redefine ${fn}`, (done) => { | ||
this.notes[fn] = 'turd' | ||
this.notes[fn]('turd') | ||
done() | ||
}) | ||
}) | ||
|
||
it("sets a top level value", (done) => { | ||
this.notes.set("foo", "bar"); | ||
// console.log(this.notes) | ||
assert.equal(this.notes.foo, "bar"); | ||
done(); | ||
}); | ||
it('sets a top level value', (done) => { | ||
this.notes.set('foo', 'bar') | ||
// console.log(this.notes) | ||
assert.equal(this.notes.foo, 'bar') | ||
done() | ||
}) | ||
|
||
it("can set a false value", (done) => { | ||
this.notes.set("boolean", false); | ||
assert.equal(this.notes.boolean, false); | ||
done(); | ||
}); | ||
it('can set a false value', (done) => { | ||
this.notes.set('boolean', false) | ||
assert.equal(this.notes.boolean, false) | ||
done() | ||
}) | ||
|
||
it("gets a top level value", (done) => { | ||
this.notes.set("foo", "bar"); | ||
assert.equal(this.notes.get("foo"), "bar"); | ||
done(); | ||
}); | ||
it('gets a top level value', (done) => { | ||
this.notes.set('foo', 'bar') | ||
assert.equal(this.notes.get('foo'), 'bar') | ||
done() | ||
}) | ||
|
||
it("sets/gets a second level value", (done) => { | ||
this.notes.set("seg1.seg2", "bar"); | ||
assert.equal(this.notes.seg1.seg2, "bar"); | ||
assert.equal(this.notes.get("seg1.seg2"), "bar"); | ||
done(); | ||
}); | ||
it('sets/gets a second level value', (done) => { | ||
this.notes.set('seg1.seg2', 'bar') | ||
assert.equal(this.notes.seg1.seg2, 'bar') | ||
assert.equal(this.notes.get('seg1.seg2'), 'bar') | ||
done() | ||
}) | ||
|
||
it("sets/gets a three level value", (done) => { | ||
this.notes.set("one.two.three", "floor"); | ||
assert.equal(this.notes.one.two.three, "floor"); | ||
assert.equal(this.notes.get("one.two.three"), "floor"); | ||
done(); | ||
}); | ||
it('sets/gets a three level value', (done) => { | ||
this.notes.set('one.two.three', 'floor') | ||
assert.equal(this.notes.one.two.three, 'floor') | ||
assert.equal(this.notes.get('one.two.three'), 'floor') | ||
done() | ||
}) | ||
|
||
it("supports array syntax", (done) => { | ||
this.notes.set(["one", "two", "three"], "floor"); | ||
assert.equal(this.notes.one.two.three, "floor"); | ||
assert.equal(this.notes.get(["one", "two", "three"]), "floor"); | ||
done(); | ||
}); | ||
it('supports array syntax', (done) => { | ||
this.notes.set(['one', 'two', 'three'], 'floor') | ||
assert.equal(this.notes.one.two.three, 'floor') | ||
assert.equal(this.notes.get(['one', 'two', 'three']), 'floor') | ||
done() | ||
}) | ||
|
||
it("array syntax tolerates dots", (done) => { | ||
this.notes.set(["one", "two", "three.four"], "floor"); | ||
assert.equal(this.notes.one.two["three.four"], "floor"); | ||
assert.equal(this.notes.get(["one", "two", "three.four"]), "floor"); | ||
done(); | ||
}); | ||
}); | ||
it('array syntax tolerates dots', (done) => { | ||
this.notes.set(['one', 'two', 'three.four'], 'floor') | ||
assert.equal(this.notes.one.two['three.four'], 'floor') | ||
assert.equal(this.notes.get(['one', 'two', 'three.four']), 'floor') | ||
done() | ||
}) | ||
}) | ||
|
||
describe("notes + object", () => { | ||
it("assigns instantiation object", (done) => { | ||
const passIn = { | ||
one: true, | ||
two: "false", | ||
three: "floor", | ||
}; | ||
this.notes = this.notes = new Notes(passIn); | ||
assert.deepEqual(this.notes, passIn); | ||
done(); | ||
}); | ||
}); | ||
describe('notes + object', () => { | ||
it('assigns instantiation object', (done) => { | ||
const passIn = { | ||
one: true, | ||
two: 'false', | ||
three: 'floor', | ||
} | ||
this.notes = this.notes = new Notes(passIn) | ||
assert.deepEqual(this.notes, passIn) | ||
done() | ||
}) | ||
}) |