|
| 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 | +} |
0 commit comments