@ohos-ports/stdlib-object 0.0.0-stage → 0.4.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +262 -2
- package/SECURITY.md +5 -0
- package/any-in-by/README.md +217 -0
- package/any-in-by/docs/repl.txt +41 -0
- package/any-in-by/docs/types/index.d.ts +102 -0
- package/any-in-by/docs/types/test.ts +53 -0
- package/any-in-by/lib/index.js +50 -0
- package/any-in-by/lib/main.js +75 -0
- package/any-in-by/package.json +66 -0
- package/any-own-by/README.md +221 -0
- package/any-own-by/docs/repl.txt +43 -0
- package/any-own-by/docs/types/index.d.ts +101 -0
- package/any-own-by/docs/types/test.ts +63 -0
- package/any-own-by/lib/index.js +46 -0
- package/any-own-by/lib/main.js +77 -0
- package/any-own-by/package.json +70 -0
- package/assign/README.md +139 -0
- package/assign/docs/repl.txt +34 -0
- package/assign/docs/types/index.d.ts +113 -0
- package/assign/docs/types/test.ts +47 -0
- package/assign/lib/builtin.js +55 -0
- package/assign/lib/has_object_assign.js +33 -0
- package/assign/lib/index.js +52 -0
- package/assign/lib/polyfill.js +86 -0
- package/assign/package.json +64 -0
- package/assign-in/README.md +145 -0
- package/assign-in/docs/repl.txt +34 -0
- package/assign-in/docs/types/index.d.ts +175 -0
- package/assign-in/docs/types/test.ts +44 -0
- package/assign-in/lib/index.js +54 -0
- package/assign-in/lib/main.js +92 -0
- package/assign-in/package.json +60 -0
- package/bifurcate-in/README.md +298 -0
- package/bifurcate-in/docs/repl.txt +68 -0
- package/bifurcate-in/docs/types/index.d.ts +189 -0
- package/bifurcate-in/docs/types/test.ts +97 -0
- package/bifurcate-in/lib/index.js +106 -0
- package/bifurcate-in/lib/main.js +148 -0
- package/bifurcate-in/lib/return_keys.js +81 -0
- package/bifurcate-in/lib/return_pairs.js +87 -0
- package/bifurcate-in/lib/return_values.js +87 -0
- package/bifurcate-in/lib/validate.js +76 -0
- package/bifurcate-in/package.json +72 -0
- package/bifurcate-own/README.md +255 -0
- package/bifurcate-own/docs/repl.txt +63 -0
- package/bifurcate-own/docs/types/index.d.ts +183 -0
- package/bifurcate-own/docs/types/test.ts +97 -0
- package/bifurcate-own/lib/index.js +87 -0
- package/bifurcate-own/lib/main.js +127 -0
- package/bifurcate-own/lib/return_keys.js +81 -0
- package/bifurcate-own/lib/return_pairs.js +87 -0
- package/bifurcate-own/lib/return_values.js +87 -0
- package/bifurcate-own/lib/validate.js +76 -0
- package/bifurcate-own/package.json +71 -0
- package/capitalize-keys/README.md +139 -0
- package/capitalize-keys/docs/repl.txt +28 -0
- package/capitalize-keys/docs/types/index.d.ts +46 -0
- package/capitalize-keys/docs/types/test.ts +33 -0
- package/capitalize-keys/lib/index.js +45 -0
- package/capitalize-keys/lib/main.js +69 -0
- package/capitalize-keys/package.json +76 -0
- package/common-keys/README.md +125 -0
- package/common-keys/docs/repl.txt +29 -0
- package/common-keys/docs/types/index.d.ts +70 -0
- package/common-keys/docs/types/test.ts +33 -0
- package/common-keys/lib/index.js +51 -0
- package/common-keys/lib/main.js +106 -0
- package/common-keys/package.json +70 -0
- package/common-keys-in/README.md +125 -0
- package/common-keys-in/docs/repl.txt +29 -0
- package/common-keys-in/docs/types/index.d.ts +70 -0
- package/common-keys-in/docs/types/test.ts +33 -0
- package/common-keys-in/lib/index.js +51 -0
- package/common-keys-in/lib/main.js +106 -0
- package/common-keys-in/package.json +72 -0
- package/ctor/README.md +536 -0
- package/ctor/docs/repl.txt +592 -0
- package/ctor/docs/types/index.d.ts +56 -0
- package/ctor/docs/types/test.ts +33 -0
- package/ctor/lib/index.js +54 -0
- package/ctor/lib/main.js +58 -0
- package/ctor/package.json +58 -0
- package/deep-get/README.md +181 -0
- package/deep-get/docs/repl.txt +63 -0
- package/deep-get/docs/types/index.d.ts +125 -0
- package/deep-get/docs/types/test.ts +109 -0
- package/deep-get/lib/defaults.js +38 -0
- package/deep-get/lib/dget.js +57 -0
- package/deep-get/lib/factory.js +94 -0
- package/deep-get/lib/index.js +58 -0
- package/deep-get/lib/main.js +101 -0
- package/deep-get/lib/validate.js +66 -0
- package/deep-get/package.json +68 -0
- package/deep-set/README.md +273 -0
- package/deep-set/docs/repl.txt +87 -0
- package/deep-set/docs/types/index.d.ts +180 -0
- package/deep-set/docs/types/test.ts +134 -0
- package/deep-set/lib/defaults.js +39 -0
- package/deep-set/lib/dset.js +80 -0
- package/deep-set/lib/factory.js +99 -0
- package/deep-set/lib/index.js +58 -0
- package/deep-set/lib/main.js +120 -0
- package/deep-set/lib/validate.js +74 -0
- package/deep-set/package.json +69 -0
- package/docs/types/index.d.ts +964 -0
- package/every-in-by/README.md +156 -0
- package/every-in-by/docs/repl.txt +43 -0
- package/every-in-by/docs/types/index.d.ts +83 -0
- package/every-in-by/docs/types/test.ts +60 -0
- package/every-in-by/lib/index.js +51 -0
- package/every-in-by/lib/main.js +70 -0
- package/every-in-by/package.json +67 -0
- package/every-own-by/README.md +223 -0
- package/every-own-by/docs/repl.txt +43 -0
- package/every-own-by/docs/types/index.d.ts +102 -0
- package/every-own-by/docs/types/test.ts +62 -0
- package/every-own-by/lib/index.js +46 -0
- package/every-own-by/lib/main.js +77 -0
- package/every-own-by/package.json +68 -0
- package/for-in/README.md +218 -0
- package/for-in/docs/repl.txt +44 -0
- package/for-in/docs/types/index.d.ts +65 -0
- package/for-in/docs/types/test.ts +50 -0
- package/for-in/lib/index.js +54 -0
- package/for-in/lib/main.js +82 -0
- package/for-in/package.json +74 -0
- package/for-own/README.md +209 -0
- package/for-own/docs/repl.txt +49 -0
- package/for-own/docs/types/index.d.ts +63 -0
- package/for-own/docs/types/test.ts +50 -0
- package/for-own/lib/index.js +50 -0
- package/for-own/lib/main.js +85 -0
- package/for-own/package.json +71 -0
- package/inverse/README.md +163 -0
- package/inverse/docs/repl.txt +53 -0
- package/inverse/docs/types/index.d.ts +66 -0
- package/inverse/docs/types/test.ts +60 -0
- package/inverse/lib/index.js +59 -0
- package/inverse/lib/main.js +123 -0
- package/inverse/package.json +69 -0
- package/inverse-by/README.md +205 -0
- package/inverse-by/docs/repl.txt +67 -0
- package/inverse-by/docs/types/index.d.ts +160 -0
- package/inverse-by/docs/types/test.ts +81 -0
- package/inverse-by/lib/index.js +78 -0
- package/inverse-by/lib/main.js +148 -0
- package/inverse-by/package.json +75 -0
- package/lib/index.js +276 -0
- package/lowercase-keys/README.md +139 -0
- package/lowercase-keys/docs/repl.txt +28 -0
- package/lowercase-keys/docs/types/index.d.ts +46 -0
- package/lowercase-keys/docs/types/test.ts +33 -0
- package/lowercase-keys/lib/index.js +45 -0
- package/lowercase-keys/lib/main.js +63 -0
- package/lowercase-keys/package.json +74 -0
- package/move-property/README.md +144 -0
- package/move-property/docs/repl.txt +38 -0
- package/move-property/docs/types/index.d.ts +53 -0
- package/move-property/docs/types/test.ts +46 -0
- package/move-property/lib/index.js +46 -0
- package/move-property/lib/main.js +74 -0
- package/move-property/package.json +69 -0
- package/none-in-by/README.md +241 -0
- package/none-in-by/docs/repl.txt +42 -0
- package/none-in-by/docs/types/index.d.ts +101 -0
- package/none-in-by/docs/types/test.ts +61 -0
- package/none-in-by/lib/index.js +46 -0
- package/none-in-by/lib/main.js +71 -0
- package/none-in-by/package.json +68 -0
- package/none-own-by/README.md +177 -0
- package/none-own-by/docs/repl.txt +42 -0
- package/none-own-by/docs/types/index.d.ts +102 -0
- package/none-own-by/docs/types/test.ts +61 -0
- package/none-own-by/lib/index.js +50 -0
- package/none-own-by/lib/main.js +75 -0
- package/none-own-by/package.json +68 -0
- package/package.json +66 -4
- package/some-in-by/README.md +232 -0
- package/some-in-by/docs/repl.txt +45 -0
- package/some-in-by/docs/types/index.d.ts +104 -0
- package/some-in-by/docs/types/test.ts +66 -0
- package/some-in-by/lib/index.js +46 -0
- package/some-in-by/lib/main.js +87 -0
- package/some-in-by/package.json +75 -0
- package/some-own-by/README.md +251 -0
- package/some-own-by/docs/repl.txt +45 -0
- package/some-own-by/docs/types/index.d.ts +103 -0
- package/some-own-by/docs/types/test.ts +66 -0
- package/some-own-by/lib/index.js +46 -0
- package/some-own-by/lib/main.js +85 -0
- package/some-own-by/package.json +72 -0
- package/uncapitalize-keys/README.md +139 -0
- package/uncapitalize-keys/docs/repl.txt +28 -0
- package/uncapitalize-keys/docs/types/index.d.ts +46 -0
- package/uncapitalize-keys/docs/types/test.ts +33 -0
- package/uncapitalize-keys/lib/index.js +45 -0
- package/uncapitalize-keys/lib/main.js +69 -0
- package/uncapitalize-keys/package.json +76 -0
- package/uppercase-keys/README.md +139 -0
- package/uppercase-keys/docs/repl.txt +28 -0
- package/uppercase-keys/docs/types/index.d.ts +46 -0
- package/uppercase-keys/docs/types/test.ts +33 -0
- package/uppercase-keys/lib/index.js +45 -0
- package/uppercase-keys/lib/main.js +63 -0
- package/uppercase-keys/package.json +74 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 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 isString = require( '@ohos-ports/stdlib-assert/is-string' ).isPrimitive;
|
|
24
|
+
var isArray = require( '@ohos-ports/stdlib-assert/is-array' );
|
|
25
|
+
var isObjectLike = require( '@ohos-ports/stdlib-assert/is-object-like' );
|
|
26
|
+
var format = require( '@ohos-ports/stdlib-string/format' );
|
|
27
|
+
var validate = require( './validate.js' );
|
|
28
|
+
var defaults = require( './defaults.js' );
|
|
29
|
+
var dget = require( './dget.js' );
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
// MAIN //
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Returns a function for retrieving a nested property value.
|
|
36
|
+
*
|
|
37
|
+
* @param {(string|Array)} path - key path
|
|
38
|
+
* @param {Options} [options] - function options
|
|
39
|
+
* @param {string} [options.sep='.'] - key path separator
|
|
40
|
+
* @throws {TypeError} first argument must be a string or key array
|
|
41
|
+
* @throws {TypeError} options argument must be an object
|
|
42
|
+
* @throws {TypeError} must provide valid options
|
|
43
|
+
* @returns {Function} deep get function
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* var dget = factory( 'a/b/c', {
|
|
47
|
+
* 'sep': '/'
|
|
48
|
+
* });
|
|
49
|
+
*/
|
|
50
|
+
function factory( path, options ) {
|
|
51
|
+
var isStr;
|
|
52
|
+
var props;
|
|
53
|
+
var opts;
|
|
54
|
+
var err;
|
|
55
|
+
isStr = isString( path );
|
|
56
|
+
if ( !isStr && !isArray( path ) ) {
|
|
57
|
+
throw new TypeError( format( 'invalid argument. Key path must be a string or a key array. Value: `%s`.', path ) );
|
|
58
|
+
}
|
|
59
|
+
opts = defaults();
|
|
60
|
+
if ( arguments.length > 1 ) {
|
|
61
|
+
err = validate( opts, options );
|
|
62
|
+
if ( err ) {
|
|
63
|
+
throw err;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if ( isStr ) {
|
|
67
|
+
props = path.split( opts.sep );
|
|
68
|
+
} else {
|
|
69
|
+
props = path;
|
|
70
|
+
}
|
|
71
|
+
return deepGet;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Returns a nested property value.
|
|
75
|
+
*
|
|
76
|
+
* @private
|
|
77
|
+
* @param {ObjectLike} obj - input object
|
|
78
|
+
* @returns {*} nested property value
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* var obj = { 'a': { 'b': { 'c': 'd' } } };
|
|
82
|
+
* var val = deepGet( obj );
|
|
83
|
+
*/
|
|
84
|
+
function deepGet( obj ) {
|
|
85
|
+
if ( isObjectLike( obj ) ) {
|
|
86
|
+
return dget( obj, props );
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
// EXPORTS //
|
|
93
|
+
|
|
94
|
+
module.exports = factory;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 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
|
+
* Get a nested property value.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/object/deep-get
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var deepGet = require( '@ohos-ports/stdlib-object/deep-get' );
|
|
28
|
+
*
|
|
29
|
+
* var obj = { 'a': { 'b': { 'c': 'd' } } };
|
|
30
|
+
*
|
|
31
|
+
* var val = deepGet( obj, 'a.b.c' );
|
|
32
|
+
* // returns 'd'
|
|
33
|
+
*
|
|
34
|
+
* var dget = factory( 'a/b/c', {
|
|
35
|
+
* 'sep': '/'
|
|
36
|
+
* });
|
|
37
|
+
*
|
|
38
|
+
* obj = { 'a': { 'b': { 'c': 'd' } } };
|
|
39
|
+
*
|
|
40
|
+
* val = dget( obj );
|
|
41
|
+
* // returns 'd'
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
// MODULES //
|
|
45
|
+
|
|
46
|
+
var setReadOnly = require( '@ohos-ports/stdlib-utils/define-nonenumerable-read-only-property' );
|
|
47
|
+
var main = require( './main.js' );
|
|
48
|
+
var factory = require( './factory.js' );
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
// MAIN //
|
|
52
|
+
|
|
53
|
+
setReadOnly( main, 'factory', factory );
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
// EXPORTS //
|
|
57
|
+
|
|
58
|
+
module.exports = main;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 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 isObjectLike = require( '@ohos-ports/stdlib-assert/is-object-like' );
|
|
24
|
+
var isString = require( '@ohos-ports/stdlib-assert/is-string' ).isPrimitive;
|
|
25
|
+
var isArray = require( '@ohos-ports/stdlib-assert/is-array' );
|
|
26
|
+
var format = require( '@ohos-ports/stdlib-string/format' );
|
|
27
|
+
var validate = require( './validate.js' );
|
|
28
|
+
var defaults = require( './defaults.js' );
|
|
29
|
+
var dget = require( './dget.js' );
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
// MAIN //
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Returns a nested property value.
|
|
36
|
+
*
|
|
37
|
+
* @param {ObjectLike} obj - input object
|
|
38
|
+
* @param {(string|Array)} path - key path
|
|
39
|
+
* @param {Options} [options] - function options
|
|
40
|
+
* @param {string} [options.sep='.'] - key path separator
|
|
41
|
+
* @throws {TypeError} second argument must be a string or key array
|
|
42
|
+
* @throws {TypeError} options argument must be an object
|
|
43
|
+
* @throws {TypeError} must provide valid options
|
|
44
|
+
* @returns {*} nested property value
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* var obj = { 'a': { 'b': { 'c': 'd' } } };
|
|
48
|
+
* var val = deepGet( obj, 'a.b.c' );
|
|
49
|
+
* // returns 'd'
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* var arr = [
|
|
53
|
+
* { 'a': [ {'x': 5} ] },
|
|
54
|
+
* { 'a': [ {'x': 10} ] }
|
|
55
|
+
* ];
|
|
56
|
+
* var val = deepGet( arr, '1.a.0.x' );
|
|
57
|
+
* // returns 10
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* var obj = { 'a': { 'b': { 'c': 'd' } } };
|
|
61
|
+
* var val = deepGet( obj, ['a','b','c'] );
|
|
62
|
+
* // returns 'd'
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* var obj = { 'a': { 'b': { 'c': 'd' } } };
|
|
66
|
+
* var val = deepGet( obj, 'a/b/c', {
|
|
67
|
+
* 'sep': '/'
|
|
68
|
+
* });
|
|
69
|
+
* // returns 'd'
|
|
70
|
+
*/
|
|
71
|
+
function deepGet( obj, path, options ) {
|
|
72
|
+
var isStr;
|
|
73
|
+
var props;
|
|
74
|
+
var opts;
|
|
75
|
+
var err;
|
|
76
|
+
if ( !isObjectLike( obj ) ) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
isStr = isString( path );
|
|
80
|
+
if ( !isStr && !isArray( path ) ) {
|
|
81
|
+
throw new TypeError( format( 'invalid argument. Key path must be a string or a key array. Value: `%s`.', path ) );
|
|
82
|
+
}
|
|
83
|
+
opts = defaults();
|
|
84
|
+
if ( arguments.length > 2 ) {
|
|
85
|
+
err = validate( opts, options );
|
|
86
|
+
if ( err ) {
|
|
87
|
+
throw err;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if ( isStr ) {
|
|
91
|
+
props = path.split( opts.sep );
|
|
92
|
+
} else {
|
|
93
|
+
props = path;
|
|
94
|
+
}
|
|
95
|
+
return dget( obj, props );
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
// EXPORTS //
|
|
100
|
+
|
|
101
|
+
module.exports = deepGet;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 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 isString = require( '@ohos-ports/stdlib-assert/is-string' ).isPrimitive;
|
|
24
|
+
var isObject = require( '@ohos-ports/stdlib-assert/is-plain-object' );
|
|
25
|
+
var hasOwnProp = require( '@ohos-ports/stdlib-assert/has-own-property' );
|
|
26
|
+
var format = require( '@ohos-ports/stdlib-string/format' );
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
// MAIN //
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Validates function options.
|
|
33
|
+
*
|
|
34
|
+
* @private
|
|
35
|
+
* @param {Object} opts - destination for function options
|
|
36
|
+
* @param {Options} options - function options
|
|
37
|
+
* @param {string} [options.sep] - key path separator
|
|
38
|
+
* @returns {(Error|null)} error or null
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* var opts = {};
|
|
42
|
+
* var options = {
|
|
43
|
+
* 'sep': '/'
|
|
44
|
+
* };
|
|
45
|
+
* var err = validate( opts, options );
|
|
46
|
+
* if ( err ) {
|
|
47
|
+
* throw err;
|
|
48
|
+
* }
|
|
49
|
+
*/
|
|
50
|
+
function validate( opts, options ) {
|
|
51
|
+
if ( !isObject( options ) ) {
|
|
52
|
+
return new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
|
|
53
|
+
}
|
|
54
|
+
if ( hasOwnProp( options, 'sep' ) ) {
|
|
55
|
+
opts.sep = options.sep;
|
|
56
|
+
if ( !isString( opts.sep ) ) {
|
|
57
|
+
return new TypeError( format( 'invalid option. `%s` option must be a string. Option: `%s`.', 'sep', opts.sep ) );
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
// EXPORTS //
|
|
65
|
+
|
|
66
|
+
module.exports = validate;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/object/deep-get",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Get a nested property value.",
|
|
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
|
+
"benchmark": "./benchmark",
|
|
19
|
+
"doc": "./docs",
|
|
20
|
+
"example": "./examples",
|
|
21
|
+
"lib": "./lib",
|
|
22
|
+
"test": "./test"
|
|
23
|
+
},
|
|
24
|
+
"types": "./docs/types",
|
|
25
|
+
"scripts": {},
|
|
26
|
+
"homepage": "https://github.com/stdlib-js/stdlib",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git://github.com/stdlib-js/stdlib.git"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {},
|
|
35
|
+
"devDependencies": {},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=0.10.0",
|
|
38
|
+
"npm": ">2.7.0"
|
|
39
|
+
},
|
|
40
|
+
"os": [
|
|
41
|
+
"aix",
|
|
42
|
+
"darwin",
|
|
43
|
+
"freebsd",
|
|
44
|
+
"linux",
|
|
45
|
+
"macos",
|
|
46
|
+
"openbsd",
|
|
47
|
+
"sunos",
|
|
48
|
+
"win32",
|
|
49
|
+
"windows"
|
|
50
|
+
],
|
|
51
|
+
"keywords": [
|
|
52
|
+
"stdlib",
|
|
53
|
+
"stdutils",
|
|
54
|
+
"stdutil",
|
|
55
|
+
"utilities",
|
|
56
|
+
"utility",
|
|
57
|
+
"utils",
|
|
58
|
+
"util",
|
|
59
|
+
"deep",
|
|
60
|
+
"get",
|
|
61
|
+
"object",
|
|
62
|
+
"obj",
|
|
63
|
+
"array",
|
|
64
|
+
"nested",
|
|
65
|
+
"property",
|
|
66
|
+
"prop"
|
|
67
|
+
]
|
|
68
|
+
}
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
|
|
3
|
+
@license Apache-2.0
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2018 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
|
+
# Deep Set
|
|
22
|
+
|
|
23
|
+
> Set a nested property value.
|
|
24
|
+
|
|
25
|
+
<section class="usage">
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
var deepSet = require( '@stdlib/object/deep-set' );
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### deepSet( obj, path, value\[, options] )
|
|
34
|
+
|
|
35
|
+
Sets a nested property value.
|
|
36
|
+
|
|
37
|
+
<!-- eslint-disable object-curly-newline, object-curly-spacing -->
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
var obj = { 'a': { 'b': { 'c': 'd' } } };
|
|
41
|
+
|
|
42
|
+
var bool = deepSet( obj, 'a.b.c', 'beep' );
|
|
43
|
+
// returns true
|
|
44
|
+
|
|
45
|
+
console.log( obj );
|
|
46
|
+
// => { 'a': { 'b': { 'c': 'beep' } } }
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
If the function is able to deep set a nested property, the function returns `true`; otherwise, the function returns `false`.
|
|
50
|
+
|
|
51
|
+
<!-- eslint-disable object-curly-newline, object-curly-spacing -->
|
|
52
|
+
|
|
53
|
+
```javascript
|
|
54
|
+
var obj = { 'a': { 'b': { 'c': 'd' } } };
|
|
55
|
+
|
|
56
|
+
var bool = deepSet( obj, 'a.b.c', 'woot' );
|
|
57
|
+
// returns true
|
|
58
|
+
|
|
59
|
+
bool = deepSet( obj, 'a.beep.c', 'boop' );
|
|
60
|
+
// returns false
|
|
61
|
+
|
|
62
|
+
bool = deepSet( null, 'a.beep.c', 'boop' );
|
|
63
|
+
// returns false
|
|
64
|
+
|
|
65
|
+
bool = deepSet( 'bap', 'a.beep.c', 'boop' );
|
|
66
|
+
// returns false
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
For `paths` including `arrays`, specify the numeric index.
|
|
70
|
+
|
|
71
|
+
<!-- eslint-disable object-curly-newline, object-curly-spacing -->
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
var arr = [
|
|
75
|
+
{ 'a': [ { 'x': 5 } ] },
|
|
76
|
+
{ 'a': [ { 'x': 10 } ] }
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
var bool = deepSet( arr, '1.a.0.x', 25 );
|
|
80
|
+
// returns true
|
|
81
|
+
|
|
82
|
+
console.log( arr );
|
|
83
|
+
/* =>
|
|
84
|
+
[
|
|
85
|
+
{ 'a': [ { 'x': 5 } ] },
|
|
86
|
+
{ 'a': [ { 'x': 25 } ] }
|
|
87
|
+
]
|
|
88
|
+
*/
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
The key `path` may be specified as either a delimited `string` or a key `array`.
|
|
92
|
+
|
|
93
|
+
<!-- eslint-disable object-curly-newline, object-curly-spacing -->
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
var obj = { 'a': { 'b': { 'c': 'd' } } };
|
|
97
|
+
|
|
98
|
+
var bool = deepSet( obj, [ 'a', 'b', 'c' ], 'beep' ); // obj => { 'a': { 'b': { 'c': 'beep' } } }
|
|
99
|
+
// returns true
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
If `value` is a `function`, the argument is treated as a `callback` and should return a value to set.
|
|
103
|
+
|
|
104
|
+
<!-- eslint-disable object-curly-newline, object-curly-spacing -->
|
|
105
|
+
|
|
106
|
+
```javascript
|
|
107
|
+
function set( val ) {
|
|
108
|
+
var ch = val;
|
|
109
|
+
var i;
|
|
110
|
+
for ( i = 0; i < 4; i++ ) {
|
|
111
|
+
val += ch;
|
|
112
|
+
}
|
|
113
|
+
return val;
|
|
114
|
+
}
|
|
115
|
+
var obj = { 'a': { 'b': { 'c': 'd' } } };
|
|
116
|
+
|
|
117
|
+
var bool = deepSet( obj, 'a.b.c', set ); // obj => { 'a': { 'b': { 'c': 'ddddd' } } }
|
|
118
|
+
// returns true
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
The function accepts the following `options`:
|
|
122
|
+
|
|
123
|
+
- **sep**: key path separator. Default: `'.'`.
|
|
124
|
+
- **create**: `boolean` indicating whether to create a path if the key path does not already exist. Default: `false`.
|
|
125
|
+
|
|
126
|
+
By default, the function assumes `dot` separated key values. To specify an alternative separator, set the `sep` option.
|
|
127
|
+
|
|
128
|
+
<!-- eslint-disable object-curly-newline, object-curly-spacing -->
|
|
129
|
+
|
|
130
|
+
```javascript
|
|
131
|
+
var obj = { 'a': { 'b': { 'c': 'd' } } };
|
|
132
|
+
|
|
133
|
+
var bool = deepSet( obj, 'a/b/c', 'beep', {
|
|
134
|
+
'sep': '/'
|
|
135
|
+
});
|
|
136
|
+
// returns true
|
|
137
|
+
|
|
138
|
+
console.log( obj );
|
|
139
|
+
// => { 'a': { 'b': { 'c': 'beep' } } }
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
To create a key path which does not already exist, set the `create` option to `true`.
|
|
143
|
+
|
|
144
|
+
<!-- eslint-disable object-curly-newline, object-curly-spacing -->
|
|
145
|
+
|
|
146
|
+
```javascript
|
|
147
|
+
var obj = { 'a': { 'b': { 'c': 'd' } } };
|
|
148
|
+
|
|
149
|
+
var bool = deepSet( obj, 'a.e.c', 'boop', {
|
|
150
|
+
'create': true
|
|
151
|
+
});
|
|
152
|
+
// returns true
|
|
153
|
+
|
|
154
|
+
console.log( obj );
|
|
155
|
+
/* =>
|
|
156
|
+
{
|
|
157
|
+
'a': {
|
|
158
|
+
'b': {
|
|
159
|
+
'c': 'beep'
|
|
160
|
+
},
|
|
161
|
+
'e': {
|
|
162
|
+
'c': 'boop'
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
*/
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
#### deepSet.factory( path\[, options] )
|
|
170
|
+
|
|
171
|
+
Creates a reusable deep set function. The factory method ensures a `deepSet` function is configured identically by using the same set of provided `options`.
|
|
172
|
+
|
|
173
|
+
```javascript
|
|
174
|
+
var dset = deepSet.factory( 'a/b/c', {
|
|
175
|
+
'create': true,
|
|
176
|
+
'sep': '/'
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
#### dset( obj, value )
|
|
181
|
+
|
|
182
|
+
Sets a nested property value.
|
|
183
|
+
|
|
184
|
+
<!-- eslint-disable object-curly-newline, object-curly-spacing -->
|
|
185
|
+
|
|
186
|
+
```javascript
|
|
187
|
+
var dset = deepSet.factory( 'a.b.c' );
|
|
188
|
+
|
|
189
|
+
var obj = { 'a': { 'b': { 'c': 'd' } } };
|
|
190
|
+
|
|
191
|
+
var bool = dset( obj, 'beep' );
|
|
192
|
+
// returns true
|
|
193
|
+
|
|
194
|
+
console.log( obj );
|
|
195
|
+
// => { 'a': { 'b': { 'c': 'beep' } } }
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
</section>
|
|
199
|
+
|
|
200
|
+
<!-- /.usage -->
|
|
201
|
+
|
|
202
|
+
<section class="examples">
|
|
203
|
+
|
|
204
|
+
## Examples
|
|
205
|
+
|
|
206
|
+
<!-- eslint no-undef: "error" -->
|
|
207
|
+
|
|
208
|
+
```javascript
|
|
209
|
+
var randu = require( '@stdlib/random/base/randu' );
|
|
210
|
+
var deepSet = require( '@stdlib/object/deep-set' );
|
|
211
|
+
|
|
212
|
+
var data;
|
|
213
|
+
var bool;
|
|
214
|
+
var keys;
|
|
215
|
+
var i;
|
|
216
|
+
|
|
217
|
+
function set( val ) {
|
|
218
|
+
return val * 10.0;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
data = new Array( 100 );
|
|
222
|
+
for ( i = 0; i < data.length; i++ ) {
|
|
223
|
+
data[ i ] = {
|
|
224
|
+
'x': Date.now(),
|
|
225
|
+
'y': [ randu(), randu(), i ]
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
keys = [ 0, 'y', 2 ];
|
|
230
|
+
for ( i = 0; i < data.length; i++ ) {
|
|
231
|
+
keys[ 0 ] = i;
|
|
232
|
+
bool = deepSet( data, keys, set );
|
|
233
|
+
if ( !bool ) {
|
|
234
|
+
console.error( 'Unable to deep set value.' );
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
console.log( data );
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
</section>
|
|
241
|
+
|
|
242
|
+
<!-- /.examples -->
|
|
243
|
+
|
|
244
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
245
|
+
|
|
246
|
+
<section class="related">
|
|
247
|
+
|
|
248
|
+
* * *
|
|
249
|
+
|
|
250
|
+
## See Also
|
|
251
|
+
|
|
252
|
+
- <span class="package-name">[`@stdlib/object/deep-get`][@stdlib/object/deep-get]</span><span class="delimiter">: </span><span class="description">get a nested property value.</span>
|
|
253
|
+
- <span class="package-name">[`@stdlib/utils/deep-pluck`][@stdlib/utils/deep-pluck]</span><span class="delimiter">: </span><span class="description">extract a nested property value from each element of an object array.</span>
|
|
254
|
+
|
|
255
|
+
</section>
|
|
256
|
+
|
|
257
|
+
<!-- /.related -->
|
|
258
|
+
|
|
259
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
260
|
+
|
|
261
|
+
<section class="links">
|
|
262
|
+
|
|
263
|
+
<!-- <related-links> -->
|
|
264
|
+
|
|
265
|
+
[@stdlib/object/deep-get]: https://github.com/stdlib-js/object/tree/main/deep-get
|
|
266
|
+
|
|
267
|
+
[@stdlib/utils/deep-pluck]: https://www.npmjs.com/package/@stdlib/utils-deep-pluck
|
|
268
|
+
|
|
269
|
+
<!-- </related-links> -->
|
|
270
|
+
|
|
271
|
+
</section>
|
|
272
|
+
|
|
273
|
+
<!-- /.links -->
|