Skip to content

Commit 93d67c0

Browse files
committed
Add Typescript definition
1 parent 0d2adbb commit 93d67c0

File tree

3 files changed

+256
-0
lines changed

3 files changed

+256
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
interface Options {
22+
/**
23+
* Boolean indicating whether to deep copy.
24+
*/
25+
copy?: boolean;
26+
27+
/**
28+
* Maximum depth to flatten.
29+
*/
30+
depth?: number;
31+
32+
/**
33+
* Boolean indicating whether to flatten arrays.
34+
*/
35+
flattenArrays?: boolean;
36+
37+
/**
38+
* Key path delimiter.
39+
*/
40+
delimiter?: string;
41+
}
42+
43+
/**
44+
* Flattens an object.
45+
*
46+
* @param obj - object to flatten
47+
* @returns flattened object
48+
*/
49+
type Unary = ( obj: any ) => any;
50+
51+
52+
/**
53+
* Interface for the flattenObject function.
54+
*/
55+
interface FlattenObject {
56+
/**
57+
* Flattens an object.
58+
*
59+
* @param obj - object to flatten
60+
* @param options - function options
61+
* @param options.depth - maximum depth to flatten
62+
* @param options.copy - boolean indicating whether to deep copy (default: false)
63+
* @param options.flattenArrays - boolean indicating whether to flatten arrays (default: false)
64+
* @param options.delimiter - key path delimiter (default: '.')
65+
* @returns flattened object
66+
*
67+
* @example
68+
* var obj = {'a':{'b':{'c':'d'}}};
69+
*
70+
* var out = flattenObject( obj );
71+
* // returns {'a.b.c':'d'}
72+
*/
73+
( obj: any, options?: Options ): any;
74+
75+
/**
76+
* Returns a function to flatten an object.
77+
*
78+
* @param options - function options
79+
* @param options.depth - maximum depth to flatten
80+
* @param options.copy - boolean indicating whether to deep copy (default: false)
81+
* @param options.flattenArrays - boolean indicating whether to flatten arrays (default: false)
82+
* @param options.delimiter - key path delimiter (default: '.')
83+
* @returns flatten function
84+
*
85+
* @example
86+
* var flatten = flattenObject.factory({
87+
* 'copy': true,
88+
* 'delimiter': '|'
89+
* });
90+
*
91+
* var obj = {'a':{'b':{'c':'d'}}};
92+
* var out = flatten( obj );
93+
* // returns {'a|b|c':'d'}
94+
*/
95+
factory( options?: Options ): Unary;
96+
}
97+
98+
declare var flattenObject: FlattenObject;
99+
100+
101+
// EXPORTS //
102+
103+
export = flattenObject;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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 flattenObject = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an object...
25+
{
26+
const obj = { 'a': { 'b': { 'c': 'd' } } };
27+
flattenObject( obj ); // $ExpectType any
28+
}
29+
30+
// The compiler throws an error if the function is provided a second argument which is not an object...
31+
{
32+
const obj = { 'a': { 'b': { 'c': 'd' } } };
33+
flattenObject( obj, null ); // $ExpectError
34+
}
35+
36+
// The compiler throws an error if the function is provided a `copy` option which is not a boolean...
37+
{
38+
const obj = { 'a': { 'b': { 'c': 'd' } } };
39+
flattenObject( obj, { 'copy': '5' } ); // $ExpectError
40+
flattenObject( obj, { 'copy': 123 } ); // $ExpectError
41+
flattenObject( obj, { 'copy': null } ); // $ExpectError
42+
flattenObject( obj, { 'copy': [] } ); // $ExpectError
43+
flattenObject( obj, { 'copy': {} } ); // $ExpectError
44+
flattenObject( obj, { 'copy': ( x: number ): number => x } ); // $ExpectError
45+
}
46+
47+
// The compiler throws an error if the function is provided a `depth` option which is not a number...
48+
{
49+
const obj = { 'a': { 'b': { 'c': 'd' } } };
50+
flattenObject( obj, { 'depth': true } ); // $ExpectError
51+
flattenObject( obj, { 'depth': false } ); // $ExpectError
52+
flattenObject( obj, { 'depth': 'abc' } ); // $ExpectError
53+
flattenObject( obj, { 'depth': null } ); // $ExpectError
54+
flattenObject( obj, { 'depth': [] } ); // $ExpectError
55+
flattenObject( obj, { 'depth': {} } ); // $ExpectError
56+
flattenObject( obj, { 'depth': ( x: number ): number => x } ); // $ExpectError
57+
}
58+
59+
// The compiler throws an error if the function is provided a `flattenArrays` option which is not a boolean...
60+
{
61+
const obj = { 'a': { 'b': { 'c': 'd' } } };
62+
flattenObject( obj, { 'flattenArrays': '5' } ); // $ExpectError
63+
flattenObject( obj, { 'flattenArrays': 123 } ); // $ExpectError
64+
flattenObject( obj, { 'flattenArrays': null } ); // $ExpectError
65+
flattenObject( obj, { 'flattenArrays': [] } ); // $ExpectError
66+
flattenObject( obj, { 'flattenArrays': {} } ); // $ExpectError
67+
flattenObject( obj, { 'flattenArrays': ( x: number ): number => x } ); // $ExpectError
68+
}
69+
70+
// The compiler throws an error if the function is provided a `delimiter` option which is not a string...
71+
{
72+
const obj = { 'a': { 'b': { 'c': 'd' } } };
73+
flattenObject( obj, { 'delimiter': true } ); // $ExpectError
74+
flattenObject( obj, { 'delimiter': false } ); // $ExpectError
75+
flattenObject( obj, { 'delimiter': 123 } ); // $ExpectError
76+
flattenObject( obj, { 'delimiter': null } ); // $ExpectError
77+
flattenObject( obj, { 'delimiter': [] } ); // $ExpectError
78+
flattenObject( obj, { 'delimiter': {} } ); // $ExpectError
79+
flattenObject( obj, { 'delimiter': ( x: number ): number => x } ); // $ExpectError
80+
}
81+
82+
// The compiler throws an error if the function is provided no arguments...
83+
{
84+
flattenObject(); // $ExpectError
85+
}
86+
87+
// Attached to main export is a `factory` method which returns a function...
88+
{
89+
flattenObject.factory(); // $ExpectType Unary
90+
}
91+
92+
// The `factory` method returns a function which returns an object...
93+
{
94+
const obj = { 'a': { 'b': { 'c': 'd' } } };
95+
const fcn = flattenObject.factory();
96+
fcn( obj ); // $ExpectType any
97+
}
98+
99+
// The compiler throws an error if the `factory` method is provided an argument which is not an object...
100+
{
101+
flattenObject.factory( null ); // $ExpectError
102+
}
103+
104+
// The compiler throws an error if the `factory` method is provided a `copy` option which is not a boolean...
105+
{
106+
flattenObject.factory( { 'copy': '5' } ); // $ExpectError
107+
flattenObject.factory( { 'copy': 123 } ); // $ExpectError
108+
flattenObject.factory( { 'copy': null } ); // $ExpectError
109+
flattenObject.factory( { 'copy': [] } ); // $ExpectError
110+
flattenObject.factory( { 'copy': {} } ); // $ExpectError
111+
flattenObject.factory( { 'copy': ( x: number ): number => x } ); // $ExpectError
112+
}
113+
114+
// The compiler throws an error if the `factory` method is provided a `flattenArrays` option which is not a boolean...
115+
{
116+
flattenObject.factory( { 'flattenArrays': '5' } ); // $ExpectError
117+
flattenObject.factory( { 'flattenArrays': 123 } ); // $ExpectError
118+
flattenObject.factory( { 'flattenArrays': null } ); // $ExpectError
119+
flattenObject.factory( { 'flattenArrays': [] } ); // $ExpectError
120+
flattenObject.factory( { 'flattenArrays': {} } ); // $ExpectError
121+
flattenObject.factory( { 'flattenArrays': ( x: number ): number => x } ); // $ExpectError
122+
}
123+
124+
// The compiler throws an error if the `factory` method is provided a `depth` option which is not a number...
125+
{
126+
flattenObject.factory( { 'depth': '5' } ); // $ExpectError
127+
flattenObject.factory( { 'depth': true } ); // $ExpectError
128+
flattenObject.factory( { 'depth': false } ); // $ExpectError
129+
flattenObject.factory( { 'depth': null } ); // $ExpectError
130+
flattenObject.factory( { 'depth': [] } ); // $ExpectError
131+
flattenObject.factory( { 'depth': {} } ); // $ExpectError
132+
flattenObject.factory( { 'depth': ( x: number ): number => x } ); // $ExpectError
133+
}
134+
135+
// The compiler throws an error if the `factory` method is provided a `delimiter` option which is not a string...
136+
{
137+
flattenObject.factory( { 'delimiter': 123 } ); // $ExpectError
138+
flattenObject.factory( { 'delimiter': true } ); // $ExpectError
139+
flattenObject.factory( { 'delimiter': false } ); // $ExpectError
140+
flattenObject.factory( { 'delimiter': null } ); // $ExpectError
141+
flattenObject.factory( { 'delimiter': [] } ); // $ExpectError
142+
flattenObject.factory( { 'delimiter': {} } ); // $ExpectError
143+
flattenObject.factory( { 'delimiter': ( x: number ): number => x } ); // $ExpectError
144+
}
145+
146+
// The compiler throws an error if the function returned by the `factory` method is provided an unsupported number of arguments...
147+
{
148+
const obj = { 'a': { 'b': { 'c': 'd' } } };
149+
const fcn = flattenObject.factory();
150+
fcn(); // $ExpectError
151+
fcn( obj, 2 ); // $ExpectError
152+
}

lib/node_modules/@stdlib/utils/flatten-object/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"lib": "./lib",
2121
"test": "./test"
2222
},
23+
"types": "./docs/types",
2324
"scripts": {},
2425
"homepage": "https://github.com/stdlib-js/stdlib",
2526
"repository": {

0 commit comments

Comments
 (0)