Skip to content

Commit 2d21f52

Browse files
committed
feat: add plot/vega/signal/reference
1 parent a256141 commit 2d21f52

10 files changed

Lines changed: 516 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 SignalReference = require( './../lib' );
22+
23+
var signal = new SignalReference( 'width' );
24+
console.log( signal.toJSON() );
25+
// => { 'signal': 'width' }
26+
27+
signal = new SignalReference( 'width / 2' );
28+
console.log( signal.toJSON() );
29+
// => { 'signal': 'width / 2' }
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
// MAIN //
22+
23+
/**
24+
* Returns a new change event object.
25+
*
26+
* @private
27+
* @param {string} property - property name
28+
* @returns {Object} event object
29+
*/
30+
function event( property ) { // eslint-disable-line stdlib/no-redeclare
31+
return {
32+
'type': 'update',
33+
'source': 'signal-reference',
34+
'property': property
35+
};
36+
}
37+
38+
39+
// EXPORTS //
40+
41+
module.exports = event;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/**
22+
* SignalReference constructor.
23+
*
24+
* @module @stdlib/plot/vega/signal/reference
25+
*
26+
* @example
27+
* var SignalReference = require( '@stdlib/plot/vega/signal/reference' );
28+
*
29+
* var signal = new SignalReference( 'width' );
30+
* // returns <SignalReference>
31+
*/
32+
33+
// MODULES //
34+
35+
var main = require( './main.js' );
36+
37+
38+
// EXPORTS //
39+
40+
module.exports = main;
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/* eslint-disable no-restricted-syntax, no-invalid-this */
20+
21+
'use strict';
22+
23+
// MODULES //
24+
25+
var EventEmitter = require( 'events' ).EventEmitter;
26+
var logger = require( 'debug' );
27+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
28+
var setNonEnumerableReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
29+
var setNonEnumerableReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' ); // eslint-disable-line id-length
30+
var setReadWriteAccessor = require( '@stdlib/utils/define-read-write-accessor' );
31+
var inherit = require( '@stdlib/utils/inherit' );
32+
var instance2json = require( '@stdlib/plot/vega/base/to-json' );
33+
var transformErrorMessage = require( '@stdlib/plot/vega/base/transform-validation-message' );
34+
var format = require( '@stdlib/string/format' );
35+
var properties = require( './properties.json' );
36+
37+
// Note: keep the following in alphabetical order according to the `require` path...
38+
var getProperties = require( './properties/get.js' );
39+
40+
var getSignal = require( './signal/get.js' );
41+
var setSignal = require( './signal/set.js' );
42+
43+
44+
// VARIABLES //
45+
46+
var debug = logger( 'vega:signal-reference:main' );
47+
48+
49+
// MAIN //
50+
51+
/**
52+
* SignalReference constructor.
53+
*
54+
* @constructor
55+
* @param {string} reference - signal reference string
56+
* @throws {TypeError} must provide a string
57+
* @returns {SignalReference} signal reference instance
58+
*
59+
* @example
60+
* var signal = new SignalReference( 'width' );
61+
* // returns <SignalReference>
62+
*
63+
* @example
64+
* var signal = new SignalReference( 'width / 2' );
65+
* // returns <SignalReference>
66+
*/
67+
function SignalReference( reference ) {
68+
if ( !( this instanceof SignalReference ) ) {
69+
return new SignalReference( reference );
70+
}
71+
EventEmitter.call( this );
72+
if ( !isString( reference ) ) {
73+
throw new TypeError( format( 'invalid argument. Must provide a string. Value: `%s`.', reference ) );
74+
}
75+
try {
76+
this.signal = reference;
77+
} catch ( err ) {
78+
debug( 'Encountered an error. Error: %s', err.message );
79+
80+
// FIXME: retain thrown error type
81+
throw new Error( transformErrorMessage( err.message ) );
82+
}
83+
return this;
84+
}
85+
86+
/*
87+
* Inherit from the `EventEmitter` prototype.
88+
*/
89+
inherit( SignalReference, EventEmitter );
90+
91+
/**
92+
* Constructor name.
93+
*
94+
* @private
95+
* @name name
96+
* @memberof SignalReference
97+
* @readonly
98+
* @type {string}
99+
*/
100+
setNonEnumerableReadOnly( SignalReference, 'name', 'SignalReference' );
101+
102+
/**
103+
* Signal reference string.
104+
*
105+
* @name signal
106+
* @memberof SignalReference.prototype
107+
* @type {string}
108+
*
109+
* @example
110+
* var signal = new SignalReference( 'width' );
111+
*
112+
* var v = signal.signal;
113+
* // returns 'width'
114+
*
115+
* @example
116+
* var signal = new SignalReference( 'width / 2' );
117+
*
118+
* var v = signal.signal;
119+
* // returns 'width / 2'
120+
*/
121+
setReadWriteAccessor( SignalReference.prototype, 'signal', getSignal, setSignal );
122+
123+
/**
124+
* SignalReference properties.
125+
*
126+
* @name properties
127+
* @memberof SignalReference.prototype
128+
* @type {Array<string>}
129+
*
130+
* @example
131+
* var signal = new SignalReference( 'width' );
132+
*
133+
* var v = signal.properties;
134+
* // returns [...]
135+
*/
136+
setNonEnumerableReadOnlyAccessor( SignalReference.prototype, 'properties', getProperties );
137+
138+
/**
139+
* Serializes an instance to a JSON object.
140+
*
141+
* ## Notes
142+
*
143+
* - This method is implicitly invoked by `JSON.stringify`.
144+
*
145+
* @name toJSON
146+
* @memberof SignalReference.prototype
147+
* @type {Function}
148+
* @returns {Object} JSON object
149+
*
150+
* @example
151+
* var signal = new SignalReference( 'width' );
152+
*
153+
* var v = signal.toJSON();
154+
* // returns { 'signal': 'width' }
155+
*/
156+
setNonEnumerableReadOnly( SignalReference.prototype, 'toJSON', function toJSON() {
157+
return instance2json( this, properties );
158+
});
159+
160+
161+
// EXPORTS //
162+
163+
module.exports = SignalReference;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"signal"
3+
]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 properties = require( './../properties.json' );
24+
25+
26+
// MAIN //
27+
28+
/**
29+
* Returns the list of enumerable properties.
30+
*
31+
* @private
32+
* @returns {Array<string>} properties
33+
*/
34+
function get() {
35+
return properties.slice();
36+
}
37+
38+
39+
// EXPORTS //
40+
41+
module.exports = get;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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+
/* eslint-disable no-invalid-this */
20+
21+
'use strict';
22+
23+
// VARIABLES //
24+
25+
var prop = require( './properties.js' );
26+
27+
28+
// MAIN //
29+
30+
/**
31+
* Returns the signal reference.
32+
*
33+
* @private
34+
* @returns {string} signal reference
35+
*/
36+
function get() {
37+
return this[ prop.private ];
38+
}
39+
40+
41+
// EXPORTS //
42+
43+
module.exports = get;

0 commit comments

Comments
 (0)