Comparing version 1.1.0 to 1.2.0
@@ -0,1 +1,2 @@ | ||
/// <reference lib="es2020.promise" /> | ||
import { Nullish } from './types'; | ||
@@ -18,2 +19,20 @@ /** Checks whether a value is a boolean */ | ||
export declare function isDefined<T>(value: T): value is NonNullable<T>; | ||
/** | ||
* Checks whether a result from `Promise.allSettled` is fulfilled | ||
* | ||
* ```ts | ||
* const results = await Promise.allSettled(promises); | ||
* const fulfilledValues = results.filter(isFulfilled).map(result => result.value); | ||
* ``` | ||
*/ | ||
export declare function isFulfilled<T>(result: PromiseSettledResult<T>): result is PromiseFulfilledResult<T>; | ||
/** | ||
* Checks whether a result from `Promise.allSettled` is rejected | ||
* | ||
* ```ts | ||
* const results = await Promise.allSettled(promises); | ||
* const rejectionReasons = results.filter(isRejected).map(result => result.reason); | ||
* ``` | ||
*/ | ||
export declare function isRejected(result: PromiseSettledResult<unknown>): result is PromiseRejectedResult; | ||
//# sourceMappingURL=guards.d.ts.map |
@@ -5,2 +5,3 @@ 'use strict'; | ||
/// <reference lib="es2020.promise" /> | ||
// Primitives | ||
@@ -51,6 +52,33 @@ // ------------------------------ | ||
return !isNullish(value); | ||
} // Promise | ||
// ------------------------------ | ||
/** | ||
* Checks whether a result from `Promise.allSettled` is fulfilled | ||
* | ||
* ```ts | ||
* const results = await Promise.allSettled(promises); | ||
* const fulfilledValues = results.filter(isFulfilled).map(result => result.value); | ||
* ``` | ||
*/ | ||
function isFulfilled(result) { | ||
return result.status === 'fulfilled'; | ||
} | ||
/** | ||
* Checks whether a result from `Promise.allSettled` is rejected | ||
* | ||
* ```ts | ||
* const results = await Promise.allSettled(promises); | ||
* const rejectionReasons = results.filter(isRejected).map(result => result.reason); | ||
* ``` | ||
*/ | ||
function isRejected(result) { | ||
return result.status === 'rejected'; | ||
} | ||
exports.isBoolean = isBoolean; | ||
exports.isDefined = isDefined; | ||
exports.isFulfilled = isFulfilled; | ||
exports.isNonEmptyArray = isNonEmptyArray; | ||
@@ -60,3 +88,4 @@ exports.isNull = isNull; | ||
exports.isNumber = isNumber; | ||
exports.isRejected = isRejected; | ||
exports.isString = isString; | ||
exports.isUndefined = isUndefined; |
@@ -5,2 +5,3 @@ 'use strict'; | ||
/// <reference lib="es2020.promise" /> | ||
// Primitives | ||
@@ -51,6 +52,33 @@ // ------------------------------ | ||
return !isNullish(value); | ||
} // Promise | ||
// ------------------------------ | ||
/** | ||
* Checks whether a result from `Promise.allSettled` is fulfilled | ||
* | ||
* ```ts | ||
* const results = await Promise.allSettled(promises); | ||
* const fulfilledValues = results.filter(isFulfilled).map(result => result.value); | ||
* ``` | ||
*/ | ||
function isFulfilled(result) { | ||
return result.status === 'fulfilled'; | ||
} | ||
/** | ||
* Checks whether a result from `Promise.allSettled` is rejected | ||
* | ||
* ```ts | ||
* const results = await Promise.allSettled(promises); | ||
* const rejectionReasons = results.filter(isRejected).map(result => result.reason); | ||
* ``` | ||
*/ | ||
function isRejected(result) { | ||
return result.status === 'rejected'; | ||
} | ||
exports.isBoolean = isBoolean; | ||
exports.isDefined = isDefined; | ||
exports.isFulfilled = isFulfilled; | ||
exports.isNonEmptyArray = isNonEmptyArray; | ||
@@ -60,3 +88,4 @@ exports.isNull = isNull; | ||
exports.isNumber = isNumber; | ||
exports.isRejected = isRejected; | ||
exports.isString = isString; | ||
exports.isUndefined = isUndefined; |
@@ -0,1 +1,2 @@ | ||
/// <reference lib="es2020.promise" /> | ||
// Primitives | ||
@@ -46,4 +47,30 @@ // ------------------------------ | ||
return !isNullish(value); | ||
} // Promise | ||
// ------------------------------ | ||
/** | ||
* Checks whether a result from `Promise.allSettled` is fulfilled | ||
* | ||
* ```ts | ||
* const results = await Promise.allSettled(promises); | ||
* const fulfilledValues = results.filter(isFulfilled).map(result => result.value); | ||
* ``` | ||
*/ | ||
function isFulfilled(result) { | ||
return result.status === 'fulfilled'; | ||
} | ||
/** | ||
* Checks whether a result from `Promise.allSettled` is rejected | ||
* | ||
* ```ts | ||
* const results = await Promise.allSettled(promises); | ||
* const rejectionReasons = results.filter(isRejected).map(result => result.reason); | ||
* ``` | ||
*/ | ||
export { isBoolean, isDefined, isNonEmptyArray, isNull, isNullish, isNumber, isString, isUndefined }; | ||
function isRejected(result) { | ||
return result.status === 'rejected'; | ||
} | ||
export { isBoolean, isDefined, isFulfilled, isNonEmptyArray, isNull, isNullish, isNumber, isRejected, isString, isUndefined }; |
{ | ||
"name": "emery", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Utilities to help polish the rough parts of TypeScript.", | ||
@@ -39,3 +39,5 @@ "main": "dist/emery.cjs.js", | ||
"docs:build": "next build docs", | ||
"prepublishOnly": "preconstruct build" | ||
"prepublishOnly": "preconstruct build", | ||
"release": "changeset publish", | ||
"changeset": "changeset" | ||
}, | ||
@@ -80,4 +82,6 @@ "jest": { | ||
"ts-jest": "^28.0.2", | ||
"typescript": "^4.6.4" | ||
"typescript": "^4.6.4", | ||
"@changesets/changelog-github": "^0.4.5", | ||
"@changesets/cli": "^2.23.0" | ||
} | ||
} |
# Emery | ||
Utilities to help polish the rough parts of TypeScript. | ||
💎 Polish for the rough parts of TypeScript. | ||
TypeScript is great but there's parts that are still rough around the edges, especially for developers who are new to the language. | ||
## Purpose and intent | ||
Emery is a small collection of utilities that improve DX without compromising static types. | ||
### Check for ambiguous types | ||
Emery exposes "checks" for dealing with ambiguous types. | ||
Checks are just predicates that can't be expressed as type guards, without enforcing opaque types. | ||
```ts | ||
const isNonNegativeInteger = checkAll(isNonNegative, isInteger); | ||
import { checkAll, isNonNegative, isInteger } from 'emery'; | ||
/** | ||
* Along with some default check functions, we provide helpers | ||
* for managing combinations. The `checkAll` helper is a bit | ||
* like `pipe` for predicates. | ||
*/ | ||
export const isNonNegativeInteger = checkAll(isNonNegative, isInteger); | ||
``` | ||
### Assert the validity of props | ||
An assertion declares that a condition be true before executing subsequent code, ensuring that whatever condition is checked must be true for the remainder of the containing scope. | ||
```ts | ||
import { assert } from 'emery'; | ||
import { isNonNegativeInteger } from './path-to/check'; | ||
function getThingByIndex(index: number) { | ||
assert(isNonNegativeInteger(index), `Expected a non-negative integer, but received "${index}"`); | ||
/* ... */ | ||
assert(isNonNegativeInteger(index)); | ||
return things[index]; // 🎉 Safely use the `index` argument! | ||
} | ||
``` | ||
getThingByIndex(-1.2); | ||
// → TypeError: Expected a non-negative integer, but received "-1.2" | ||
### Smooth over loose types | ||
Utility functions for smoothing over areas of TypeScript that are loosely typed. | ||
Because of JavaScript's dynamic implementation the default TS behaviour is correct, but can be frustrating in certain situations. | ||
```ts | ||
import { typedKeys } from 'emery'; | ||
const obj = { foo: 1, bar: 2 }; | ||
const thing = Object.keys(obj).map(key => { | ||
return obj[key]; // 🚨 'string' can't be used to index... | ||
}); | ||
const thing2 = typedKeys(obj).map(key => { | ||
return obj[key]; // 🎉 No more TypeScript issues! | ||
}); | ||
``` | ||
[View docs →](https://emery-ts.vercel.app/) | ||
## Philosophy and motivation | ||
## Why? | ||
Like all good things, Emery started with curiosity. At [Thinkmill](https://thinkmill.com.au/) we have an internal Slack channel for TypeScript where a question was raised about how to offer consumers error messages that convey intent, not just cascading type failures. | ||
TypeScript is great, we enjoy using it in our projects but there's some parts that are still rough around the edges, especially for those new to the language. Emery improves the developer experience by taking the hassle out of working with ambiguous types, forked logic, error messages, etc. | ||
While that's not currently possible, it became apparent that there was demand for a solution. We also discovered that many developers were carrying around miscellaneous utilities for working with TypeScript between projects. |
import { | ||
isBoolean, | ||
isDefined, | ||
isFulfilled, | ||
isNonEmptyArray, | ||
@@ -8,2 +9,3 @@ isNull, | ||
isNumber, | ||
isRejected, | ||
isString, | ||
@@ -89,2 +91,15 @@ isUndefined, | ||
}); | ||
describe('promise', () => { | ||
it('isFulfilled should validate assumed values', async () => { | ||
expect( | ||
(await Promise.allSettled([Promise.resolve(), Promise.reject()])).map(x => isFulfilled(x)), | ||
).toEqual([true, false]); | ||
}); | ||
it('isRejected should validate assumed values', async () => { | ||
expect( | ||
(await Promise.allSettled([Promise.resolve(), Promise.reject()])).map(x => isRejected(x)), | ||
).toEqual([false, true]); | ||
}); | ||
}); | ||
}); |
@@ -0,1 +1,2 @@ | ||
/// <reference lib="es2020.promise" /> | ||
import { Nullish } from './types'; | ||
@@ -51,1 +52,30 @@ | ||
} | ||
// Promise | ||
// ------------------------------ | ||
/** | ||
* Checks whether a result from `Promise.allSettled` is fulfilled | ||
* | ||
* ```ts | ||
* const results = await Promise.allSettled(promises); | ||
* const fulfilledValues = results.filter(isFulfilled).map(result => result.value); | ||
* ``` | ||
*/ | ||
export function isFulfilled<T>( | ||
result: PromiseSettledResult<T>, | ||
): result is PromiseFulfilledResult<T> { | ||
return result.status === 'fulfilled'; | ||
} | ||
/** | ||
* Checks whether a result from `Promise.allSettled` is rejected | ||
* | ||
* ```ts | ||
* const results = await Promise.allSettled(promises); | ||
* const rejectionReasons = results.filter(isRejected).map(result => result.reason); | ||
* ``` | ||
*/ | ||
export function isRejected(result: PromiseSettledResult<unknown>): result is PromiseRejectedResult { | ||
return result.status === 'rejected'; | ||
} |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
75216
1705
68
22