Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed May 3, 2024
1 parent 7ef2ecd commit 52465d2
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 118 deletions.
82 changes: 40 additions & 42 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,58 @@
class 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),
})
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),
})
}
}

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, onlyWhenUndefined) {
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()]
}
if (onlyWhenUndefined) {
if (dest[segments[0]] === undefined)
dest[segments[0]] = value
}
else {
dest[segments[0]] = value
}
if (!dest[segments[0]]) dest[segments[0]] = {}
// set dest one path segment deeper
dest = dest[segments.shift()]
}
if (onlyWhenUndefined) {
if (dest[segments[0]] === undefined) dest[segments[0]] = value
} else {
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)
}
152 changes: 76 additions & 76 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,89 +3,89 @@ const assert = require('assert')
const Notes = require('../index')

describe('notes', () => {
beforeEach((done) => {
this.notes = new Notes()
done()
})

it('exports an object', () => {
assert.ok(typeof this.notes === 'object')
})

const functionList = ['get', 'set']

for (const fn of functionList) {
it(`has ${fn}()`, () => {
assert.equal(typeof this.notes[fn], 'function')
})
}

for (const fn of functionList) {
it(`ignores attempts to redefine ${fn}`, () => {
this.notes[fn] = 'turd'
this.notes[fn]('turd')
})
}

it('sets a top level value', () => {
this.notes.set('foo', 'bar')
assert.equal(this.notes.foo, 'bar')
})

it('can set a false value', () => {
this.notes.set('boolean', false)
assert.equal(this.notes.boolean, false)
})

it('gets a top level value', () => {
this.notes.set('foo', 'bar')
assert.equal(this.notes.get('foo'), 'bar')
})
beforeEach((done) => {
this.notes = new Notes()
done()
})

it('sets/gets a second level value', () => {
this.notes.set('seg1.seg2', 'bar')
assert.equal(this.notes.seg1.seg2, 'bar')
assert.equal(this.notes.get('seg1.seg2'), 'bar')
})
it('exports an object', () => {
assert.ok(typeof this.notes === 'object')
})

it('sets/gets a three level value', () => {
this.notes.set('one.two.three', 'floor')
assert.equal(this.notes.one.two.three, 'floor')
assert.equal(this.notes.get('one.two.three'), 'floor')
})
const functionList = ['get', 'set']

it('supports array syntax', () => {
this.notes.set(['one', 'two', 'three'], 'floor')
assert.equal(this.notes.one.two.three, 'floor')
assert.equal(this.notes.get(['one', 'two', 'three']), 'floor')
for (const fn of functionList) {
it(`has ${fn}()`, () => {
assert.equal(typeof this.notes[fn], 'function')
})
}

it('array syntax tolerates dots', () => {
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')
})

it('sets default sets a property', () => {
this.notes.set(['one', 'two'], 'tree', true)
assert.equal(this.notes.one.two, 'tree')
})

it('set default does NOT change defined property', () => {
this.notes.set('one.two', 'tree', true)
this.notes.set('one.two', 'three', true)
assert.equal(this.notes.one.two, 'tree')
for (const fn of functionList) {
it(`ignores attempts to redefine ${fn}`, () => {
this.notes[fn] = 'turd'
this.notes[fn]('turd')
})
}

it('sets a top level value', () => {
this.notes.set('foo', 'bar')
assert.equal(this.notes.foo, 'bar')
})

it('can set a false value', () => {
this.notes.set('boolean', false)
assert.equal(this.notes.boolean, false)
})

it('gets a top level value', () => {
this.notes.set('foo', 'bar')
assert.equal(this.notes.get('foo'), 'bar')
})

it('sets/gets a second level value', () => {
this.notes.set('seg1.seg2', 'bar')
assert.equal(this.notes.seg1.seg2, 'bar')
assert.equal(this.notes.get('seg1.seg2'), 'bar')
})

it('sets/gets a three level value', () => {
this.notes.set('one.two.three', 'floor')
assert.equal(this.notes.one.two.three, 'floor')
assert.equal(this.notes.get('one.two.three'), 'floor')
})

it('supports array syntax', () => {
this.notes.set(['one', 'two', 'three'], 'floor')
assert.equal(this.notes.one.two.three, 'floor')
assert.equal(this.notes.get(['one', 'two', 'three']), 'floor')
})

it('array syntax tolerates dots', () => {
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')
})

it('sets default sets a property', () => {
this.notes.set(['one', 'two'], 'tree', true)
assert.equal(this.notes.one.two, 'tree')
})

it('set default does NOT change defined property', () => {
this.notes.set('one.two', 'tree', true)
this.notes.set('one.two', 'three', true)
assert.equal(this.notes.one.two, 'tree')
})
})

describe('notes + object', () => {
it('assigns instantiation object', () => {
const passIn = {
one: true,
two: 'false',
three: 'floor',
}
this.notes = this.notes = new Notes(passIn)
assert.deepEqual(this.notes, passIn)
})
it('assigns instantiation object', () => {
const passIn = {
one: true,
two: 'false',
three: 'floor',
}
this.notes = this.notes = new Notes(passIn)
assert.deepEqual(this.notes, passIn)
})
})

0 comments on commit 52465d2

Please sign in to comment.