Skip to content

Commit e835bf4

Browse files
committed
feat: add initial plot/vega/base/to-json implementation
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent b269d9e commit e835bf4

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
* Serialize a list of private instance properties from a class instance to a JSON object.
23+
*
24+
* @module @stdlib/plot/vega/base/to-json
25+
*
26+
* @example
27+
* var toJSON = require( '@stdlib/plot/vega/base/to-json' );
28+
*
29+
* // TODO: example
30+
*/
31+
32+
// MODULES //
33+
34+
var main = require( './main.js' );
35+
36+
37+
// EXPORTS //
38+
39+
module.exports = main;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 isFunction = require( '@stdlib/assert/is-function' );
24+
var isArray = require( '@stdlib/assert/is-array' );
25+
var isObject = require( '@stdlib/assert/is-object' );
26+
var copy = require( '@stdlib/utils/copy' );
27+
28+
29+
// MAIN //
30+
31+
/**
32+
* Serializes a list of private instance properties from a class instance to a JSON object.
33+
*
34+
* @param {Object} obj - input instance
35+
* @param {Array<string>} properties - properties to serialize
36+
* @returns {Object} JSON object
37+
*/
38+
function toJSON( obj, properties ) {
39+
var out;
40+
var tmp;
41+
var v;
42+
var o;
43+
var k;
44+
var i;
45+
var j;
46+
47+
out = {};
48+
for ( i = 0; i < properties.length; i++ ) {
49+
k = properties[ i ];
50+
v = obj[ '_'+k ];
51+
if ( v === void 0 ) {
52+
continue;
53+
}
54+
if ( isArray( v ) ) {
55+
tmp = [];
56+
for ( j = 0; j < v.length; j++ ) {
57+
o = v[ j ];
58+
if ( o && isFunction( o.toJSON ) ) {
59+
tmp.push( o.toJSON() );
60+
} else if ( isObject( o ) ) {
61+
tmp.push( copy( o ) );
62+
} else {
63+
tmp.push( o );
64+
}
65+
}
66+
out[ k ] = tmp;
67+
} else if ( v && isFunction( v.toJSON ) ) {
68+
out[ k ] = v.toJSON();
69+
} else if ( isObject( v ) ) {
70+
out[ k ] = copy( v );
71+
} else {
72+
out[ k ] = v;
73+
}
74+
}
75+
return out;
76+
}
77+
78+
79+
// EXPORTS //
80+
81+
module.exports = toJSON;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"name": "@stdlib/plot/vega/base/to-json",
3+
"version": "0.0.0",
4+
"description": "Serialize a list of private instance properties from a class instance to a JSON object.",
5+
"license": "Apache-2.0",
6+
"author": {
7+
"name": "The Stdlib Authors",
8+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9+
},
10+
"contributors": [
11+
{
12+
"name": "The Stdlib Authors",
13+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14+
}
15+
],
16+
"main": "./lib",
17+
"directories": {
18+
"lib": "./lib"
19+
},
20+
"types": "./docs/types",
21+
"scripts": {},
22+
"homepage": "https://github.com/stdlib-js/stdlib",
23+
"repository": {
24+
"type": "git",
25+
"url": "git://github.com/stdlib-js/stdlib.git"
26+
},
27+
"bugs": {
28+
"url": "https://github.com/stdlib-js/stdlib/issues"
29+
},
30+
"dependencies": {},
31+
"devDependencies": {},
32+
"engines": {
33+
"node": ">=0.10.0",
34+
"npm": ">2.7.0"
35+
},
36+
"os": [
37+
"aix",
38+
"darwin",
39+
"freebsd",
40+
"linux",
41+
"macos",
42+
"openbsd",
43+
"sunos",
44+
"win32",
45+
"windows"
46+
],
47+
"keywords": [
48+
"stdlib",
49+
"plot",
50+
"vega",
51+
"json",
52+
"utilities",
53+
"utility",
54+
"utils",
55+
"util"
56+
],
57+
"__stdlib__": {}
58+
}

0 commit comments

Comments
 (0)