define-lazy-prop
Advanced tools
+33
| /** | ||
| Define a [lazily evaluated](https://en.wikipedia.org/wiki/Lazy_evaluation) property on an object. | ||
| @param object - Object to add property to. | ||
| @param propertyName - Name of the property to add. | ||
| @param fn - Called the first time `propertyName` is accessed. | ||
| @example | ||
| ``` | ||
| import defineLazyProp = require('define-lazy-prop'); | ||
| const unicorn = { | ||
| // … | ||
| }; | ||
| defineLazyProp(unicorn, 'rainbow', () => expensiveComputation()); | ||
| app.on('user-action', () => { | ||
| doSomething(unicorn.rainbow); | ||
| }); | ||
| ``` | ||
| */ | ||
| declare function defineLazyProp< | ||
| ObjectType extends {[key: string]: unknown}, | ||
| PropertyNameType extends string, | ||
| PropertyValueType | ||
| >( | ||
| object: ObjectType, | ||
| propertyName: PropertyNameType, | ||
| fn: () => PropertyValueType | ||
| ): ObjectType & {[K in PropertyNameType]: PropertyValueType}; | ||
| export = defineLazyProp; |
+9
-9
| 'use strict'; | ||
| module.exports = (obj, prop, fn) => { | ||
| const define = value => Object.defineProperty(obj, prop, {value, enumerable: true, writable: true}); | ||
| module.exports = (object, propertyName, fn) => { | ||
| const define = value => Object.defineProperty(object, propertyName, {value, enumerable: true, writable: true}); | ||
| Object.defineProperty(obj, prop, { | ||
| Object.defineProperty(object, propertyName, { | ||
| configurable: true, | ||
| enumerable: true, | ||
| get() { | ||
| const ret = fn(); | ||
| define(ret); | ||
| return ret; | ||
| const result = fn(); | ||
| define(result); | ||
| return result; | ||
| }, | ||
| set(val) { | ||
| define(val); | ||
| set(value) { | ||
| define(value); | ||
| } | ||
| }); | ||
| return obj; | ||
| return object; | ||
| }; |
+46
-46
| { | ||
| "name": "define-lazy-prop", | ||
| "version": "1.0.0", | ||
| "description": "Define a lazily evaluated property on an object", | ||
| "license": "MIT", | ||
| "repository": "sindresorhus/define-lazy-prop", | ||
| "author": { | ||
| "name": "Sindre Sorhus", | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "sindresorhus.com" | ||
| }, | ||
| "engines": { | ||
| "node": ">=4" | ||
| }, | ||
| "scripts": { | ||
| "test": "xo && ava" | ||
| }, | ||
| "files": [ | ||
| "index.js" | ||
| ], | ||
| "keywords": [ | ||
| "lazy", | ||
| "property", | ||
| "properties", | ||
| "prop", | ||
| "define", | ||
| "object", | ||
| "obj", | ||
| "value", | ||
| "lazily", | ||
| "laziness", | ||
| "val", | ||
| "evaluation", | ||
| "eval", | ||
| "execute", | ||
| "getter", | ||
| "function", | ||
| "fn", | ||
| "memoize", | ||
| "cache", | ||
| "defer", | ||
| "deferred" | ||
| ], | ||
| "devDependencies": { | ||
| "ava": "*", | ||
| "xo": "*" | ||
| } | ||
| "name": "define-lazy-prop", | ||
| "version": "2.0.0", | ||
| "description": "Define a lazily evaluated property on an object", | ||
| "license": "MIT", | ||
| "repository": "sindresorhus/define-lazy-prop", | ||
| "author": { | ||
| "name": "Sindre Sorhus", | ||
| "email": "sindresorhus@gmail.com", | ||
| "url": "sindresorhus.com" | ||
| }, | ||
| "engines": { | ||
| "node": ">=8" | ||
| }, | ||
| "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": "^1.4.1", | ||
| "tsd": "^0.7.2", | ||
| "xo": "^0.24.0" | ||
| } | ||
| } |
+7
-7
@@ -11,3 +11,3 @@ # define-lazy-prop [](https://travis-ci.org/sindresorhus/define-lazy-prop) | ||
| ``` | ||
| $ npm install --save define-lazy-prop | ||
| $ npm install define-lazy-prop | ||
| ``` | ||
@@ -22,3 +22,3 @@ | ||
| const unicorn = { | ||
| // ... | ||
| // … | ||
| }; | ||
@@ -36,5 +36,5 @@ | ||
| ### defineLazyProp(obj, prop, fn) | ||
| ### defineLazyProp(object, propertyName, fn) | ||
| #### obj | ||
| #### object | ||
@@ -45,3 +45,3 @@ Type: `Object` | ||
| #### prop | ||
| #### propertyName | ||
@@ -56,3 +56,3 @@ Type: `string` | ||
| Called the first time `prop` is accessed. Expected to return a value. | ||
| Called the first time `propertyName` is accessed. Expected to return a value. | ||
@@ -63,3 +63,3 @@ | ||
| - [lazy-value](https://github.com/sindresorhus/lazy-value) - Create a lazily evaluated value | ||
| - [lazy-req](https://github.com/sindresorhus/lazy-req) - Require modules lazily | ||
| - [import-lazy](https://github.com/sindresorhus/import-lazy) - Import a module lazily | ||
| - [p-lazy](https://github.com/sindresorhus/p-lazy) - Create a lazy promise | ||
@@ -66,0 +66,0 @@ |
Sorry, the diff of this file is not supported yet
4446
22.95%5
25%44
158.82%3
50%