UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

57 lines (56 loc) 1.88 kB
import type { AnyFunction, AnyObject, Lazy } from './types.js'; /** * const value = lazyValue(() => expensiveComputation()) * * value() // calls expensiveComputation() once * value() // returns cached result * value() // returns cached result * * Based on: https://github.com/sindresorhus/lazy-value */ export declare function _lazyValue<T>(fn: () => T): Lazy<T>; /** * interface Obj { * v: number * } * * const obj = {} as Obj * * _defineLazyProperty(obj, 'v', () => expensiveComputation()) * obj.v // runs expensiveComputation() once * obj.v // cached value * obj.v // cached value * * Based on: https://github.com/sindresorhus/define-lazy-prop */ export declare function _defineLazyProperty<OBJ extends AnyObject>(obj: OBJ, propertyName: keyof OBJ, fn: AnyFunction): OBJ; /** * Like _defineLazyProperty, but allows to define multiple props at once. */ export declare function _defineLazyProps<OBJ extends AnyObject>(obj: OBJ, props: Partial<Record<keyof OBJ, AnyFunction>>): OBJ; /** * Same as Object.defineProperty, but with better (least restricting) defaults. * * Defaults are: * writable: true * configurable: true * enumerable: true * value: existing obj[prop] value * * Original defaults: * writable: false * configurable: false * enumerable: false * value: existing obj[prop] value * */ export declare function _defineProperty<T extends AnyObject>(obj: T, prop: keyof T, pd: PropertyDescriptor): T; /** * Object.defineProperties with better defaults. * See _defineProperty for exact defaults definition. */ export declare function _defineProps<T extends AnyObject>(obj: T, props: Partial<Record<keyof T, PropertyDescriptor>>): T; /** * Like _defineProps, but skips props with nullish values. */ export declare function _defineNonNullishProps<T extends AnyObject>(obj: T, props: Partial<Record<keyof T, PropertyDescriptor>>): T;