pax_global_header00006660000000000000000000000064140355176220014517gustar00rootroot0000000000000052 comment=038c2e8088b23e81100e34e7f38d1690c631157c define-lazy-prop-3.0.0/000077500000000000000000000000001403551762200147045ustar00rootroot00000000000000define-lazy-prop-3.0.0/.editorconfig000066400000000000000000000002571403551762200173650ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 define-lazy-prop-3.0.0/.gitattributes000066400000000000000000000000231403551762200175720ustar00rootroot00000000000000* text=auto eol=lf define-lazy-prop-3.0.0/.github/000077500000000000000000000000001403551762200162445ustar00rootroot00000000000000define-lazy-prop-3.0.0/.github/workflows/000077500000000000000000000000001403551762200203015ustar00rootroot00000000000000define-lazy-prop-3.0.0/.github/workflows/main.yml000066400000000000000000000006451403551762200217550ustar00rootroot00000000000000name: CI on: - push - pull_request jobs: test: name: Node.js ${{ matrix.node-version }} runs-on: ubuntu-latest strategy: fail-fast: false matrix: node-version: - 14 - 12 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install - run: npm test define-lazy-prop-3.0.0/.gitignore000066400000000000000000000000271403551762200166730ustar00rootroot00000000000000node_modules yarn.lock define-lazy-prop-3.0.0/.npmrc000066400000000000000000000000231403551762200160170ustar00rootroot00000000000000package-lock=false define-lazy-prop-3.0.0/index.d.ts000066400000000000000000000014461403551762200166120ustar00rootroot00000000000000/** Define a [lazily evaluated](https://en.wikipedia.org/wiki/Lazy_evaluation) property on an object. @param object - Object to add the property to. @param propertyName - Name of the property to add. @param valueGetter - Called the first time `propertyName` is accessed. @example ``` import defineLazyProperty from 'define-lazy-prop'; const unicorn = { // … }; defineLazyProperty(unicorn, 'rainbow', () => expensiveComputation()); app.on('user-action', () => { doSomething(unicorn.rainbow); }); ``` */ export default function defineLazyProperty< ObjectType extends Record, PropertyNameType extends string, PropertyValueType >( object: ObjectType, propertyName: PropertyNameType, valueGetter: () => PropertyValueType ): ObjectType & {[K in PropertyNameType]: PropertyValueType}; define-lazy-prop-3.0.0/index.js000066400000000000000000000006551403551762200163570ustar00rootroot00000000000000export default function defineLazyProperty(object, propertyName, valueGetter) { const define = value => Object.defineProperty(object, propertyName, {value, enumerable: true, writable: true}); Object.defineProperty(object, propertyName, { configurable: true, enumerable: true, get() { const result = valueGetter(); define(result); return result; }, set(value) { define(value); } }); return object; } define-lazy-prop-3.0.0/index.test-d.ts000066400000000000000000000004731403551762200175660ustar00rootroot00000000000000import {expectType, expectError} from 'tsd'; import defineLazyProperty from './index.js'; const unicorn = { foo: 'bar' }; const rainbowUnicorn = defineLazyProperty(unicorn, 'rainbow', () => 1); expectType(rainbowUnicorn.rainbow); expectType(rainbowUnicorn.foo); expectError(rainbowUnicorn.bar); define-lazy-prop-3.0.0/license000066400000000000000000000021351403551762200162520ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. define-lazy-prop-3.0.0/package.json000066400000000000000000000015431403551762200171750ustar00rootroot00000000000000{ "name": "define-lazy-prop", "version": "3.0.0", "description": "Define a lazily evaluated property on an object", "license": "MIT", "repository": "sindresorhus/define-lazy-prop", "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "https://sindresorhus.com" }, "type": "module", "exports": "./index.js", "engines": { "node": ">=12" }, "scripts": { "test": "xo && ava && tsd" }, "files": [ "index.js", "index.d.ts" ], "keywords": [ "lazy", "property", "properties", "prop", "define", "object", "value", "lazily", "laziness", "evaluation", "eval", "execute", "getter", "function", "fn", "memoize", "cache", "defer", "deferred" ], "devDependencies": { "ava": "^3.15.0", "tsd": "^0.14.0", "xo": "^0.38.2" } } define-lazy-prop-3.0.0/readme.md000066400000000000000000000022541403551762200164660ustar00rootroot00000000000000# define-lazy-prop > Define a [lazily evaluated](https://en.wikipedia.org/wiki/Lazy_evaluation) property on an object Useful when the value of a property is expensive to generate, so you want to delay the computation until the property is needed. For example, improving startup performance by deferring nonessential operations. ## Install ``` $ npm install define-lazy-prop ``` ## Usage ```js import defineLazyProperty from 'define-lazy-prop'; const unicorn = { // … }; defineLazyProperty(unicorn, 'rainbow', () => expensiveComputation()); app.on('user-action', () => { doSomething(unicorn.rainbow); }); ``` ## API ### defineLazyProperty(object, propertyName, valueGetter) #### object Type: `object` Object to add the property to. #### propertyName Type: `string` Name of the property to add. #### valueGetter Type: `Function` Called the first time `propertyName` is accessed. Expected to return a value. ## Related - [lazy-value](https://github.com/sindresorhus/lazy-value) - Create a lazily evaluated value - [import-lazy](https://github.com/sindresorhus/import-lazy) - Import a module lazily - [p-lazy](https://github.com/sindresorhus/p-lazy) - Create a lazy promise define-lazy-prop-3.0.0/test.js000066400000000000000000000005131403551762200162200ustar00rootroot00000000000000import test from 'ava'; import defineLazyProperty from './index.js'; test('main', t => { const object = {}; let index = 0; defineLazyProperty(object, 'x', () => { index++; return 'foo'; }); t.is(object.x, 'foo'); t.is(object.x, 'foo'); t.is(index, 1); object.x = 'bar'; t.is(object.x, 'bar'); t.is(index, 1); });