Skip to content

Commit dcd3ae4

Browse files
gururaj1512kgryte
andauthored
feat: add plot/vega/signal/reference
PR-URL: #10564 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent ad8b73f commit dcd3ae4

File tree

10 files changed

+517
-0
lines changed

10 files changed

+517
-0
lines changed
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) 2026 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) 2026 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) 2026 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+
* Signal reference 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: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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+
* Signal reference constructor.
53+
*
54+
* @constructor
55+
* @param {string} reference - signal reference string
56+
* @throws {TypeError} must provide a string
57+
* @throws {Error} must provide valid arguments
58+
* @returns {SignalReference} signal reference instance
59+
*
60+
* @example
61+
* var signal = new SignalReference( 'width' );
62+
* // returns <SignalReference>
63+
*
64+
* @example
65+
* var signal = new SignalReference( 'width / 2' );
66+
* // returns <SignalReference>
67+
*/
68+
function SignalReference( reference ) {
69+
if ( !( this instanceof SignalReference ) ) {
70+
return new SignalReference( reference );
71+
}
72+
EventEmitter.call( this );
73+
if ( !isString( reference ) ) {
74+
throw new TypeError( format( 'invalid argument. Must provide a string. Value: `%s`.', reference ) );
75+
}
76+
try {
77+
this.signal = reference;
78+
} catch ( err ) {
79+
debug( 'Encountered an error. Error: %s', err.message );
80+
81+
// FIXME: retain thrown error type
82+
throw new Error( transformErrorMessage( err.message ) );
83+
}
84+
return this;
85+
}
86+
87+
/*
88+
* Inherit from the `EventEmitter` prototype.
89+
*/
90+
inherit( SignalReference, EventEmitter );
91+
92+
/**
93+
* Constructor name.
94+
*
95+
* @private
96+
* @name name
97+
* @memberof SignalReference
98+
* @readonly
99+
* @type {string}
100+
*/
101+
setNonEnumerableReadOnly( SignalReference, 'name', 'SignalReference' );
102+
103+
/**
104+
* Signal reference string.
105+
*
106+
* @name signal
107+
* @memberof SignalReference.prototype
108+
* @type {string}
109+
*
110+
* @example
111+
* var signal = new SignalReference( 'width' );
112+
*
113+
* var v = signal.signal;
114+
* // returns 'width'
115+
*
116+
* @example
117+
* var signal = new SignalReference( 'width / 2' );
118+
*
119+
* var v = signal.signal;
120+
* // returns 'width / 2'
121+
*/
122+
setReadWriteAccessor( SignalReference.prototype, 'signal', getSignal, setSignal );
123+
124+
/**
125+
* Signal reference properties.
126+
*
127+
* @name properties
128+
* @memberof SignalReference.prototype
129+
* @type {Array<string>}
130+
*
131+
* @example
132+
* var signal = new SignalReference( 'width' );
133+
*
134+
* var v = signal.properties;
135+
* // returns [...]
136+
*/
137+
setNonEnumerableReadOnlyAccessor( SignalReference.prototype, 'properties', getProperties );
138+
139+
/**
140+
* Serializes an instance to a JSON object.
141+
*
142+
* ## Notes
143+
*
144+
* - This method is implicitly invoked by `JSON.stringify`.
145+
*
146+
* @name toJSON
147+
* @memberof SignalReference.prototype
148+
* @type {Function}
149+
* @returns {Object} JSON object
150+
*
151+
* @example
152+
* var signal = new SignalReference( 'width' );
153+
*
154+
* var v = signal.toJSON();
155+
* // returns { 'signal': 'width' }
156+
*/
157+
setNonEnumerableReadOnly( SignalReference.prototype, 'toJSON', function toJSON() {
158+
return instance2json( this, properties );
159+
});
160+
161+
162+
// EXPORTS //
163+
164+
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) 2026 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) 2026 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)