-
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.
chore: rewrite package on typescript
- Loading branch information
oldskytree
committed
Sep 12, 2022
1 parent
0d500f4
commit 182037c
Showing
26 changed files
with
454 additions
and
323 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 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,7 +1,2 @@ | ||
const {root, section, map, option} = require('./core'); | ||
const {MissingOptionError, UnknownKeysError} = require('./errors'); | ||
|
||
module.exports = { | ||
root, section, map, option, | ||
MissingOptionError, UnknownKeysError | ||
}; | ||
export {root, section, map, option} from './core'; | ||
export {MissingOptionError, UnknownKeysError} from './errors'; |
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,38 +1,50 @@ | ||
const _ = require('lodash'); | ||
import _ from 'lodash'; | ||
|
||
const isLazy = Symbol('isLazy'); | ||
import type {LazyObject} from './types/lazy'; | ||
|
||
function buildLazyObject(keys, getKeyGetter) { | ||
export const isLazy = Symbol('isLazy'); | ||
|
||
type SimpleOrLazyObject<T> = T | LazyObject<T>; | ||
|
||
export function buildLazyObject<T>(keys: Array<keyof T>, getKeyGetter: (key: keyof T) => () => (SimpleOrLazyObject<T[keyof T]>)): LazyObject<T> { | ||
const target = { | ||
[isLazy]: true | ||
}; | ||
} as LazyObject<T>; | ||
|
||
for (const key of keys) { | ||
defineLazy(target, key, getKeyGetter(key)); | ||
} | ||
|
||
return target; | ||
} | ||
|
||
function forceParsing(lazyObject) { | ||
export function forceParsing<T>(lazyObject: LazyObject<T>): T { | ||
return _.cloneDeep(lazyObject); | ||
} | ||
|
||
function defineLazy(object, key, getter) { | ||
function defineLazy<T>(object: LazyObject<T>, key: keyof T, getter: () => SimpleOrLazyObject<T[keyof T]>): void { | ||
let defined = false; | ||
let value; | ||
let value: T[keyof T]; | ||
|
||
Object.defineProperty(object, key, { | ||
get() { | ||
get(): T[keyof T] { | ||
if (!defined) { | ||
defined = true; | ||
value = getter(); | ||
if (_.isObject(value) && value[isLazy]) { | ||
value = forceParsing(value); | ||
} | ||
const val = getter(); | ||
|
||
value = isLazyObject(val) ? forceParsing(val) : val; | ||
} | ||
|
||
return value; | ||
}, | ||
enumerable: true | ||
}); | ||
} | ||
|
||
module.exports = {forceParsing, buildLazyObject}; | ||
function isLazyObject<T>(value: T): value is LazyObject<T> { | ||
return _.isObject(value) && hasOwnProperty(value, isLazy) && value[isLazy] === true; | ||
} | ||
|
||
function hasOwnProperty<T extends object, K extends PropertyKey>(obj: T, prop: K): obj is T & Record<K, unknown> { | ||
return Object.prototype.hasOwnProperty.call(obj, 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
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,12 @@ | ||
import type {LazyObject} from './lazy'; | ||
import type {MapParser} from './map'; | ||
import type {OptionParser} from './option'; | ||
import type {SectionParser} from './section'; | ||
|
||
export type ParsedConfig<Config> = {[Key in keyof Config]: LazyObject<Config[Key]>}; | ||
|
||
export type Parser<Config, Result> = OptionParser<Config, Result> | SectionParser<Config, Result> | MapParser<Config, Result>; | ||
|
||
export interface Rooted<T> { | ||
root: T; | ||
} |
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,5 @@ | ||
import type {isLazy} from '../lazy'; | ||
|
||
export type LazyObject<T> = T & { | ||
[isLazy]: true; | ||
}; |
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,20 @@ | ||
import type {RootPrefixes, ConfigParserArg} from './root'; | ||
|
||
export type LocatorArg<Config> = RootPrefixes & ConfigParserArg<Config>; | ||
|
||
export type Prefixes = Required<RootPrefixes> & { | ||
namePrefix: string; | ||
}; | ||
|
||
export interface Node<Options> { | ||
name: string; | ||
parent: string | null; | ||
option: Options; | ||
envVar?: string; | ||
cliOption?: string; | ||
} | ||
|
||
export interface Locator<Options> extends Node<Options> { | ||
nested<Key extends keyof Options>(key: Key): Locator<Options[Key]>; | ||
resetOption(newOption: Options): Locator<Options>; | ||
} |
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,7 @@ | ||
import type {Rooted} from './common'; | ||
import type {LazyObject} from './lazy'; | ||
import type {Locator} from './locator'; | ||
|
||
export interface MapParser<Config, Result> { | ||
(locator: Locator<Config>, config: Rooted<Result>): LazyObject<Config>; | ||
} |
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,19 @@ | ||
import type {Rooted} from './common'; | ||
import type {Locator} from './locator'; | ||
|
||
interface MetaInfo { | ||
isSetByUser: boolean; | ||
} | ||
|
||
export interface OptionParserConfig<Value, MappedValue, Result> { | ||
defaultValue?: Value | ((config: Result, currNode: any) => Value); | ||
parseCli?: (input?: string) => Value | undefined; | ||
parseEnv?: (input?: string) => Value | undefined; | ||
validate?: (value: unknown, config: Result, currNode: any, meta: MetaInfo) => asserts value is Value; | ||
map?(value: Value, config: Result, currNode: any, meta: MetaInfo): MappedValue; | ||
isDeprecated?: boolean; | ||
} | ||
|
||
export interface OptionParser<Value, Result> { | ||
(locator: Locator<Value>, config: Rooted<Result>): Value; | ||
} |
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,20 @@ | ||
import type {MapParser} from './map'; | ||
import type {SectionParser} from './section'; | ||
import type {DeepPartial} from './utils'; | ||
|
||
export interface ConfigParserArg<Options> { | ||
options: Options; | ||
env: NodeJS.ProcessEnv; | ||
argv: NodeJS.Process['argv']; | ||
} | ||
|
||
export interface ConfigParser<Config> { | ||
(arg: ConfigParserArg<DeepPartial<Config>>): Config; | ||
} | ||
|
||
export interface RootPrefixes { | ||
envPrefix?: string; | ||
cliPrefix?: string; | ||
} | ||
|
||
export type RootParser<Config, Result> = SectionParser<Config, Result> | MapParser<Config, Result>; |
Oops, something went wrong.