-
Notifications
You must be signed in to change notification settings - Fork 2
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
oldskytree
committed
Nov 16, 2021
1 parent
37508fd
commit d2792ec
Showing
28 changed files
with
466 additions
and
348 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
build |
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 +1,2 @@ | ||
node_modules | ||
build |
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 |
---|---|---|
|
@@ -3,4 +3,5 @@ node_js: | |
- '6' | ||
- '8' | ||
script: | ||
- npm build | ||
- npm test |
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
This file was deleted.
Oops, something went wrong.
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 @@ | ||
export { root, section, map, option } from './core'; | ||
export { MissingOptionError, UnknownKeysError } from './errors'; |
This file was deleted.
Oops, something went wrong.
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,52 @@ | ||
import _ from 'lodash'; | ||
|
||
import type { LazyObject } from '../types/lazy'; | ||
|
||
export const isLazy = Symbol('isLazy'); | ||
|
||
export function buildLazyObject<T>(keys: Array<keyof T>, getKeyGetter: (key: keyof T) => () => (T[keyof T] | LazyObject<T[keyof T]>)): LazyObject<T> { | ||
const target = { | ||
[isLazy]: true | ||
} as LazyObject<T>; | ||
|
||
for (const key of keys) { | ||
defineLazy(target, key, getKeyGetter(key)); | ||
} | ||
|
||
return target; | ||
} | ||
|
||
export function forceParsing<T>(lazyObject: LazyObject<T>): T { | ||
return _.cloneDeep(lazyObject); | ||
} | ||
|
||
function defineLazy<T>(object: LazyObject<T>, key: keyof T, getter: () => T[keyof T] | LazyObject<T[keyof T]>): void { | ||
let defined = false; | ||
let value: T[keyof T]; | ||
|
||
Object.defineProperty(object, key, { | ||
get(): T[keyof T] { | ||
if (!defined) { | ||
defined = true; | ||
const val = getter(); | ||
|
||
if (isLazyObject(val)) { | ||
value = forceParsing(val); | ||
} else { | ||
value = val; | ||
} | ||
} | ||
|
||
return value; | ||
}, | ||
enumerable: true | ||
}); | ||
} | ||
|
||
function isLazyObject<T>(value: T): value is LazyObject<T> { | ||
return _.isObject(value) && hasOwnProperty(value, isLazy) && value[isLazy] === true; | ||
} | ||
|
||
function hasOwnProperty<T extends {}>(obj: T, prop: PropertyKey): obj is T & Record<typeof prop, unknown> { | ||
return obj.hasOwnProperty(prop); | ||
} |
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
Oops, something went wrong.