-
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
2 changed files
with
116 additions
and
118 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 |
---|---|---|
@@ -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) | ||
} |
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