Skip to content

Commit 1b09094

Browse files
committed
Add utility to define a configurable read-only accessor
1 parent 0dad968 commit 1b09094

File tree

10 files changed

+649
-0
lines changed

10 files changed

+649
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 Read-Only Accessor
22+
23+
> [Define][@stdlib/utils/define-property] a configurable **read-only** accessor.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
<!-- eslint-disable id-length -->
30+
31+
```javascript
32+
var setConfigurableReadOnlyAccessor = require( '@stdlib/utils/define-configurable-read-only-accessor' );
33+
```
34+
35+
#### setConfigurableReadOnlyAccessor( obj, prop, getter )
36+
37+
[Defines][@stdlib/utils/define-property] a configurable **read-only** accessor.
38+
39+
<!-- run throws: true -->
40+
41+
<!-- eslint-disable id-length -->
42+
43+
```javascript
44+
function getter() {
45+
return 'bar';
46+
}
47+
48+
var obj = {};
49+
50+
setConfigurableReadOnlyAccessor( obj, 'foo', getter );
51+
52+
obj.foo = 'boop';
53+
// throws <Error>
54+
```
55+
56+
</section>
57+
58+
<!-- /.usage -->
59+
60+
<section class="notes">
61+
62+
## Notes
63+
64+
- Configurable read-only accessors are **enumerable**.
65+
66+
</section>
67+
68+
<!-- /.notes -->
69+
70+
<section class="examples">
71+
72+
## Examples
73+
74+
<!-- eslint-disable id-length -->
75+
76+
<!-- eslint no-undef: "error" -->
77+
78+
```javascript
79+
var setConfigurableReadOnlyAccessor = require( '@stdlib/utils/define-configurable-read-only-accessor' );
80+
81+
function Foo( name ) {
82+
if ( !(this instanceof Foo) ) {
83+
return new Foo( name );
84+
}
85+
setConfigurableReadOnlyAccessor( this, 'name', getName );
86+
return this;
87+
88+
function getName() {
89+
return name;
90+
}
91+
}
92+
93+
var foo = new Foo( 'beep' );
94+
95+
try {
96+
foo.name = 'boop';
97+
} catch ( err ) {
98+
console.error( err.message );
99+
}
100+
```
101+
102+
</section>
103+
104+
<!-- /.examples -->
105+
106+
<section class="links">
107+
108+
[@stdlib/utils/define-property]: https://github.com/stdlib-js/stdlib
109+
110+
</section>
111+
112+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 setConfigurableReadOnlyAccessor = 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+
setConfigurableReadOnlyAccessor( obj, 'foo', getter( 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 getter( i ) {
52+
var v = fromCodePoint( 97 + (i%26) );
53+
return get;
54+
55+
function get() {
56+
return v;
57+
}
58+
}
59+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( obj, prop, getter )
3+
Defines a configurable read-only accessor.
4+
5+
Configurable read-only accessors 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+
getter: Function
16+
Get accessor.
17+
18+
Examples
19+
--------
20+
> var obj = {};
21+
> function getter() { return 'bar'; };
22+
> {{alias}}( obj, 'foo', getter );
23+
> obj.foo
24+
'bar'
25+
26+
See Also
27+
--------
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
* Getter function.
27+
*
28+
* @returns property value
29+
*/
30+
type Getter = () => any;
31+
32+
/**
33+
* Defines a configurable read-only accessor.
34+
*
35+
* ## Notes
36+
*
37+
* - Configurable read-only accessors are **enumerable**.
38+
*
39+
* @param obj - object on which to define property
40+
* @param prop - property name
41+
* @param getter - get accessor
42+
*
43+
* @example
44+
* var obj = {};
45+
*
46+
* function getter() {
47+
* return 'bar';
48+
* }
49+
*
50+
* setConfigurableReadOnlyAccessor( obj, 'foo', getter );
51+
*
52+
* try {
53+
* obj.foo = 'boop';
54+
* } catch ( err ) {
55+
* console.error( err.message );
56+
* }
57+
*/
58+
declare function setConfigurableReadOnlyAccessor( obj: any, prop: PropertyName, getter: Getter ): void; // tslint:disable-line: max-line-length
59+
60+
61+
// EXPORTS //
62+
63+
export = setConfigurableReadOnlyAccessor;
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 setConfigurableReadAccessor = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns `undefined`...
25+
{
26+
setConfigurableReadOnlyAccessor( {}, '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+
setConfigurableReadOnlyAccessor( {}, true, (): string => 'bar' ); // $ExpectError
32+
setConfigurableReadOnlyAccessor( {}, false, (): string => 'bar' ); // $ExpectError
33+
setConfigurableReadOnlyAccessor( {}, null, (): string => 'bar' ); // $ExpectError
34+
setConfigurableReadOnlyAccessor( {}, undefined, (): string => 'bar' ); // $ExpectError
35+
setConfigurableReadOnlyAccessor( {}, [], (): string => 'bar' ); // $ExpectError
36+
setConfigurableReadOnlyAccessor( {}, {}, (): string => 'bar' ); // $ExpectError
37+
setConfigurableReadOnlyAccessor( {}, ( 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 valid getter...
41+
{
42+
setConfigurableReadOnlyAccessor( {}, 'foo', '5' ); // $ExpectError
43+
setConfigurableReadOnlyAccessor( {}, 'foo', 5 ); // $ExpectError
44+
setConfigurableReadOnlyAccessor( {}, 'foo', true ); // $ExpectError
45+
setConfigurableReadOnlyAccessor( {}, 'foo', false ); // $ExpectError
46+
setConfigurableReadOnlyAccessor( {}, 'foo', null ); // $ExpectError
47+
setConfigurableReadOnlyAccessor( {}, 'foo', undefined ); // $ExpectError
48+
setConfigurableReadOnlyAccessor( {}, 'foo', [] ); // $ExpectError
49+
setConfigurableReadOnlyAccessor( {}, 'foo', {} ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided insufficient arguments...
53+
{
54+
setConfigurableReadOnlyAccessor(); // $ExpectError
55+
setConfigurableReadOnlyAccessor( {} ); // $ExpectError
56+
setConfigurableReadOnlyAccessor( {}, 'foo' ); // $ExpectError
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 setConfigurableReadOnlyAccessor = require( './../lib' ); // eslint-disable-line id-length
22+
23+
function Foo( name ) {
24+
if ( !(this instanceof Foo) ) {
25+
return new Foo( name );
26+
}
27+
setConfigurableReadOnlyAccessor( this, 'name', getName );
28+
return this;
29+
30+
function getName() {
31+
return name;
32+
}
33+
}
34+
35+
var foo = new Foo( 'beep' );
36+
37+
try {
38+
foo.name = 'boop';
39+
} catch ( err ) {
40+
console.error( err.message );
41+
}

0 commit comments

Comments
 (0)