![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@fgv/ts-utils
Advanced tools
Assorted typescript utilities that I'm tired of copying from project to project. Most notable and closest to production-ready are:
Also includes a few other much less-developed odds-and-ends borrowed from one project or another - much less polished and more likely change or disappear:
With npm:
npm install ts-utils
A Result<T> represents the success or failure of executing some operation. A successful result contains a return value of type T, while a failure result contains an error message of type string. Taken by itself, the use of Result<T> allows for simple inline error handling.
const result = functionReturningResult();
if (result.isSuccess()) {
functionAcceptingT(result.value);
}
else {
console.log(result.error);
}
Use succeed<T>() and fail<T>() to return success or failure:
function thisFunctionSucceeds(): string {
return succeed('I succeeded!');
}
function thisFunctionFails(): number {
return fail('Oops! I failed');
}
Use getValueOrDefault when a failure can be safely ignored:
// returns undefined on failure
const value1: string|undefined = functionReturningResult('whatever').getValueOrDefault();
// returns 'oops' on failure
const value2: string = functionReturningResult('whatever').getValueOrDefault('oops');
The getValueOrThrow method converts a failure result to an exception, for use in contexts (such as constructors) in which an exception is the most appropriate way to handle errors.
constructor(param: string) {
this._param = validateReturnsResult(param).getValueOrThrow();
}
The captureResult function converts an exception to a failure for simplified inline processing.
class Thing {
static create(param: string): Result<Thing> {
return captureResult(new Thing(param));
}
}
Other methods and helpers allow for chaining and conversion of results, working with mulitple results and more. See the API documentation for details.
The basic Converter<T> implements a convert method which converts unknown to T, using the result pattern to report success or failure.
class Converter<T> {
public convert(from: unknown): Result<T>;
}
But built-in converters, including converters which can extract a field for an object or which apply converters according to the shape of some object can be composed to provide compact and legible type-safe conversion from anything to a strongly typed Typescript object:
interface Thing {
title: string;
count: number;
isGood: boolean;
hints: string[];
}
const thingConverter = Converters.object<Thing>({
title: Converters.string,
count: Converters.number,
isGood: Converters.boolean,
hints: Converters.array(Converters.string),
});
// gets a Thing or throws an error
const thing: Things = thingConverter.convert(json).getValueOrThrow();
Everything is strongly-typed, so Intellisense will autocomplete properties and highlight errors in the object supplied to Converters.object.
Other helpers and methods enable optional values or fields, chaining of results and a variety of other conversions and transformations.
FAQs
Assorted Typescript Utilities
The npm package @fgv/ts-utils receives a total of 1,124 weekly downloads. As such, @fgv/ts-utils popularity was classified as popular.
We found that @fgv/ts-utils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.