Skip to content

Commit 12e0594

Browse files
committed
Add utility to define a configurable memoized read-only property
1 parent 4532dd0 commit 12e0594

File tree

10 files changed

+704
-0
lines changed

10 files changed

+704
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2019 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Configurable Memoized Read-Only
22+
23+
> [Define][mdn-define-property] a configurable memoized **read-only** object property.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
<!-- eslint-disable id-length -->
30+
31+
```javascript
32+
var setMemoizedConfigurableReadOnly = require( '@stdlib/utils/define-memoized-configurable-read-only-property' );
33+
```
34+
35+
#### setMemoizedConfigurableReadOnly( obj, prop, fcn )
36+
37+
[Defines][mdn-define-property] a configurable memoized **read-only** object property.
38+
39+
<!-- eslint-disable id-length -->
40+
41+
```javascript
42+
var obj = {};
43+
44+
function foo() {
45+
return 'bar';
46+
}
47+
48+
setMemoizedConfigurableReadOnly( obj, 'foo', foo );
49+
50+
var v = obj.foo;
51+
// returns 'bar'
52+
```
53+
54+
The last argument should be a synchronous function whose return value will be memoized and set as the property value.
55+
56+
</section>
57+
58+
<!-- /.usage -->
59+
60+
<section class="notes">
61+
62+
## Notes
63+
64+
- A configurable **read-only** property is **enumerable**.
65+
66+
</section>
67+
68+
<!-- /.notes -->
69+
70+
<section class="examples">
71+
72+
## Examples
73+
74+
<!-- eslint no-undef: "error" -->
75+
76+
<!-- eslint-disable id-length -->
77+
78+
```javascript
79+
var fibonacci = require( '@stdlib/math/base/special/fibonacci' );
80+
var setMemoizedConfigurableReadOnly = require( '@stdlib/utils/define-memoized-configurable-read-only-property' );
81+
82+
function Foo() {
83+
var self;
84+
if ( !(this instanceof Foo) ) {
85+
return new Foo();
86+
}
87+
self = this;
88+
this.count = 0;
89+
setMemoizedConfigurableReadOnly( this, 'fibo', fibo );
90+
return this;
91+
92+
function fibo() {
93+
self.count += 1;
94+
return fibonacci( 73 );
95+
}
96+
}
97+
98+
var foo = new Foo();
99+
100+
var i;
101+
for ( i = 0; i < 10; i++ ) {
102+
console.log( 'F: %d. Count: %d.', foo.fibo, foo.count );
103+
}
104+
```
105+
106+
</section>
107+
108+
<!-- /.examples -->
109+
110+
<section class="links">
111+
112+
[mdn-define-property]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
113+
114+
</section>
115+
116+
<!-- /.links -->
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
25+
var fromCodePoint = require( '@stdlib/string/from-code-point' );
26+
var pkg = require( './../package.json' ).name;
27+
var setMemoizedConfigurableReadOnly = require( './../lib' ); // eslint-disable-line id-length
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var obj;
34+
var i;
35+
36+
b.tic();
37+
for ( i = 0; i < b.iterations; i++ ) {
38+
obj = {};
39+
setMemoizedConfigurableReadOnly( obj, 'foo', f( i ) );
40+
if ( !isString( obj.foo ) ) {
41+
b.fail( 'should return a string' );
42+
}
43+
}
44+
b.toc();
45+
if ( !isString( obj.foo ) ) {
46+
b.fail( 'should return a string' );
47+
}
48+
b.pass( 'benchmark finished' );
49+
b.end();
50+
51+
function f( i ) {
52+
return fcn;
53+
54+
function fcn() {
55+
return fromCodePoint( 97 + (i%26) );
56+
}
57+
}
58+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( obj, prop, fcn )
3+
Defines a configurable memoized read-only object property.
4+
5+
Configurable read-only properties are enumerable.
6+
7+
Parameters
8+
----------
9+
obj: Object
10+
Object on which to define the property.
11+
12+
prop: string|symbol
13+
Property name.
14+
15+
fcn: Function
16+
Synchronous function whose return value will be memoized and set as the
17+
property value.
18+
19+
Examples
20+
--------
21+
> var obj = {};
22+
> function foo() { return 'bar'; };
23+
> {{alias}}( obj, 'foo', foo );
24+
> obj.foo
25+
'bar'
26+
27+
See Also
28+
--------
29+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { PropertyName } from '@stdlib/types/object';
24+
25+
/**
26+
* Returns a property value.
27+
*
28+
* @returns property value
29+
*/
30+
type Getter = () => any;
31+
32+
/**
33+
* Defines a configurable memoized read-only object property.
34+
*
35+
* ## Notes
36+
*
37+
* - Configurable read-only properties are **enumerable**.
38+
*
39+
* @param obj - object on which to define property
40+
* @param prop - property name
41+
* @param fcn - function whose return value will be memoized and set as the property value
42+
*
43+
* @example
44+
* var obj = {};
45+
*
46+
* function foo() {
47+
* return 'bar';
48+
* }
49+
*
50+
* setMemoizedConfigurableReadOnly( obj, 'foo', foo );
51+
*
52+
* var v = obj.foo;
53+
* // returns 'bar'
54+
*/
55+
declare function setMemoizedConfigurableReadOnly( obj: any, prop: PropertyName, fcn: Getter ): void; // tslint:disable-line: max-line-length
56+
57+
58+
// EXPORTS //
59+
60+
export = setMemoizedConfigurableReadOnly;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import setMemoizedConfigurableReadOnly = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns `undefined`...
25+
{
26+
setMemoizedConfigurableReadOnly( {}, 'foo', (): string => 'bar' ); // $ExpectType void
27+
}
28+
29+
// The compiler throws an error if the function is provided a second argument which is not a valid property name...
30+
{
31+
setMemoizedConfigurableReadOnly( {}, true, (): string => 'bar' ); // $ExpectError
32+
setMemoizedConfigurableReadOnly( {}, false, (): string => 'bar' ); // $ExpectError
33+
setMemoizedConfigurableReadOnly( {}, null, (): string => 'bar' ); // $ExpectError
34+
setMemoizedConfigurableReadOnly( {}, undefined, (): string => 'bar' ); // $ExpectError
35+
setMemoizedConfigurableReadOnly( {}, [], (): string => 'bar' ); // $ExpectError
36+
setMemoizedConfigurableReadOnly( {}, {}, (): string => 'bar' ); // $ExpectError
37+
setMemoizedConfigurableReadOnly( {}, ( x: number ): number => x, (): string => 'bar' ); // $ExpectError
38+
}
39+
40+
// The compiler throws an error if the function is provided a third argument which is not a getter...
41+
{
42+
setMemoizedConfigurableReadOnly( {}, 'foo', '5' ); // $ExpectError
43+
setMemoizedConfigurableReadOnly( {}, 'foo', 5 ); // $ExpectError
44+
setMemoizedConfigurableReadOnly( {}, 'foo', true ); // $ExpectError
45+
setMemoizedConfigurableReadOnly( {}, 'foo', false ); // $ExpectError
46+
setMemoizedConfigurableReadOnly( {}, 'foo', null ); // $ExpectError
47+
setMemoizedConfigurableReadOnly( {}, 'foo', undefined ); // $ExpectError
48+
setMemoizedConfigurableReadOnly( {}, 'foo', [] ); // $ExpectError
49+
setMemoizedConfigurableReadOnly( {}, 'foo', {} ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided insufficient arguments...
53+
{
54+
setMemoizedConfigurableReadOnly(); // $ExpectError
55+
setMemoizedConfigurableReadOnly( {} ); // $ExpectError
56+
setMemoizedConfigurableReadOnly( {}, 'foo' ); // $ExpectError
57+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
var fibonacci = require( '@stdlib/math/base/special/fibonacci' );
22+
var setMemoizedConfigurableReadOnly = require( './../lib' ); // eslint-disable-line id-length
23+
24+
function Foo() {
25+
var self;
26+
if ( !(this instanceof Foo) ) {
27+
return new Foo();
28+
}
29+
self = this;
30+
this.count = 0;
31+
setMemoizedConfigurableReadOnly( this, 'fibo', fibo );
32+
return this;
33+
34+
function fibo() {
35+
self.count += 1;
36+
return fibonacci( 73 );
37+
}
38+
}
39+
40+
var foo = new Foo();
41+
42+
var i;
43+
for ( i = 0; i < 10; i++ ) {
44+
console.log( 'F: %d. Count: %d.', foo.fibo, foo.count );
45+
}

0 commit comments

Comments
 (0)