Comparing version 2.5.6 to 3.0.0
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
/** | ||
* DeepRequiredArray | ||
@@ -18,3 +25,3 @@ * Nested array condition handler | ||
type DeepRequiredObject<T extends object> = { | ||
[P in keyof T]-?: DeepRequired<NonNullable<T[P]>> | ||
[P in keyof T]-?: DeepRequired<NonNullable<T[P]>>; | ||
}; | ||
@@ -41,3 +48,3 @@ | ||
? DeepRequiredObject<T> | ||
: T; | ||
: NonNullable<T>; | ||
@@ -44,0 +51,0 @@ /** |
/** | ||
* Copyright (c) 2013-present, Facebook, Inc. | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
@@ -7,8 +7,10 @@ * This source code is licensed under the MIT license found in the | ||
* | ||
* strict | ||
* | ||
* @format | ||
*/ | ||
'use strict'; // eslint-disable-line strict | ||
'use strict'; | ||
// eslint-disable-line strict | ||
/** | ||
@@ -58,3 +60,2 @@ * Traverses properties on objects and arrays. If an intermediate property is | ||
*/ | ||
function idx(input, accessor) { | ||
@@ -65,5 +66,5 @@ try { | ||
if (error instanceof TypeError) { | ||
if (nullPattern.test(error)) { | ||
if (nullPattern.test(error.message)) { | ||
return null; | ||
} else if (undefinedPattern.test(error)) { | ||
} else if (undefinedPattern.test(error.message)) { | ||
return undefined; | ||
@@ -88,4 +89,3 @@ } | ||
var undefinedPattern = /^undefined | undefined$|^[^(]* undefined /i; | ||
idx.default = idx; | ||
module.exports = idx; | ||
idx["default"] = idx; | ||
module.exports = idx; |
@@ -0,1 +1,8 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import idx, {IDXOptional} from './idx'; | ||
@@ -2,0 +9,0 @@ |
{ | ||
"name": "idx", | ||
"version": "2.5.6", | ||
"version": "3.0.0", | ||
"description": "Utility function for traversing properties on objects and arrays.", | ||
@@ -11,3 +11,7 @@ "main": "lib/idx.js", | ||
], | ||
"repository": "facebookincubator/idx", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/facebook/idx.git", | ||
"directory": "packages/idx" | ||
}, | ||
"license": "MIT", | ||
@@ -17,13 +21,16 @@ "scripts": { | ||
"prepublish": "yarn run build && cp ../../README.md .", | ||
"test": "jest && yarn run tsc", | ||
"tsc": "tsc --noEmit --strict src/idx.test.ts" | ||
"test": "jest idx.test.js && yarn run tsc", | ||
"tsc": "tsc --noEmit --strict src/idx.test.ts", | ||
"upgrade": "yarn upgrade --latest" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.23.0", | ||
"babel-jest": "^19.0.0", | ||
"babel-plugin-transform-flow-strip-types": "^6.22.0", | ||
"babel-preset-env": "^1.1.11", | ||
"flow-bin": "^0.92.1", | ||
"jest": "^19.0.2", | ||
"typescript": "^3.3.3333" | ||
"@babel/cli": "^7.21.0", | ||
"@babel/core": "^7.21.4", | ||
"@babel/plugin-transform-flow-strip-types": "^7.21.0", | ||
"@babel/preset-env": "^7.21.4", | ||
"@babel/preset-typescript": "^7.21.4", | ||
"babel-jest": "^29.5.0", | ||
"flow-bin": "^0.211.0", | ||
"jest": "^29.5.0", | ||
"typescript": "^5.0.4" | ||
}, | ||
@@ -33,4 +40,3 @@ "jest": { | ||
"rootDir": "src" | ||
}, | ||
"gitHead": "431291a19453512a5a22ec0750cea381912fa48a" | ||
} | ||
} |
@@ -1,12 +0,14 @@ | ||
# idx [![Circle Status](https://circleci.com/gh/facebookincubator/idx/tree/master.svg?style=shield&circle-token=da61f3cf105f22309c8ca0ba4482daa538bf5349)](https://circleci.com/gh/facebookincubator/idx) | ||
# idx [![CircleCI](https://dl.circleci.com/status-badge/img/gh/facebook/idx/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/facebook/idx/tree/main) | ||
`idx` is a utility function for traversing properties on objects and arrays. | ||
[![Support Ukraine](https://img.shields.io/badge/Support-Ukraine-FFD500?style=flat&labelColor=005BBB)](https://opensource.fb.com/support-ukraine) | ||
If an intermediate property is either null or undefined, it is instead returned. | ||
The purpose of this function is to simplify extracting properties from a chain | ||
of maybe-typed properties. | ||
`idx` is a utility function for traversing properties on objects and arrays, | ||
where intermediate properties may be null or undefined. | ||
This module exists as a stop-gap solution because JavaScript does not currently | ||
support [optional chaining](https://github.com/tc39/proposal-optional-chaining). | ||
**This module has since been deprecated, in favor of [optional chaining](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining).** | ||
One notable difference between `idx` and optional chaining is what happens when | ||
an intermediate property is null or undefined. With `idx`, the null or undefined | ||
value is returned, whereas optional chaining would resolve to undefined. | ||
## Install | ||
@@ -29,5 +31,3 @@ | ||
{ | ||
plugins: [ | ||
["babel-plugin-idx"] | ||
] | ||
plugins: [['babel-plugin-idx']]; | ||
} | ||
@@ -48,3 +48,3 @@ ``` | ||
friends: ?Array<User>, | ||
} | ||
}, | ||
}; | ||
@@ -57,5 +57,5 @@ ``` | ||
props.user && | ||
props.user.friends && | ||
props.user.friends[0] && | ||
props.user.friends[0].friends | ||
props.user.friends && | ||
props.user.friends[0] && | ||
props.user.friends[0].friends; | ||
``` | ||
@@ -66,3 +66,3 @@ | ||
```javascript | ||
idx(props, _ => _.user.friends[0].friends) | ||
idx(props, _ => _.user.friends[0].friends); | ||
``` | ||
@@ -88,2 +88,10 @@ | ||
**If you use `idx@3+`,** you may need to add the following to your `.flowconfig`: | ||
``` | ||
[options] | ||
conditional_type=true | ||
mapped_type=true | ||
``` | ||
## Babel Plugin | ||
@@ -103,4 +111,4 @@ | ||
function getFriends() { | ||
return idx(props, _ => _.user.friends[0].friends) | ||
}; | ||
return idx(props, _ => _.user.friends[0].friends); | ||
} | ||
``` | ||
@@ -112,6 +120,9 @@ | ||
function getFriends() { | ||
props.user == null ? props.user : | ||
props.user.friends == null ? props.user.friends : | ||
props.user.friends[0] == null ? props.user.friends[0] : | ||
return props.user.friends[0].friends | ||
return props.user == null | ||
? props.user | ||
: props.user.friends == null | ||
? props.user.friends | ||
: props.user.friends[0] == null | ||
? props.user.friends[0] | ||
: props.user.friends[0].friends; | ||
} | ||
@@ -128,6 +139,9 @@ ``` | ||
plugins: [ | ||
["babel-plugin-idx", { | ||
importName: './idx', | ||
}] | ||
] | ||
[ | ||
'babel-plugin-idx', | ||
{ | ||
importName: './idx', | ||
}, | ||
], | ||
]; | ||
} | ||
@@ -134,0 +148,0 @@ ``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
15723
353
145
0
9