Git Product home page Git Product logo

toolbox's Introduction

Silvermine Toolbox - TypeScript Types and Utilities

Tools such as a protractor, calculator, pencil, and a pen inside a shirt pocket.

NPM Version License Build Status Coverage Status Dependency Status Dev Dependency Status Conventional Commits

What?

A collection of common types, type guards, and utilities for our TypeScript codebases.

Why?

We found that in many TypeScript projects we needed some of the same basic constructs, e.g. an object that had string keys and string values. These types are easy enough to define in each project (e.g. { [k: string]: string }), but then you might need a type guard, too. And if you need a type guard, you need unit tests for that type guard. So, we collect frequently-used types in this repo, and use this project in our other projects.

While we wanted to focus primarily on types, and we don't want this to become the "junk drawer" of every tool we ever need, there was also the need for a few helper / utility functions in our codebases. For example, in some codebases we don't want to use Underscore or lodash because we only need one or two functions and can't afford the extra bloat of a full-blown library comprised of mostly things we don't need; those functions will be implemented in this toolbox. This codebase is not a replacement for Underscore or lodash; it's primarily a collection of types and type guards, with a few additional utilities sprinkled in.

More details about the specific types and utilities will be included at a later time when we get a doc-build system integrated.

License

This software is released under the MIT license. See the license file for more details.

toolbox's People

Contributors

crgwbr avatar jmbutler27 avatar jthomerson avatar lukelafountaine avatar onebytegone avatar pbredenberg avatar renovate[bot] avatar yokuze avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

toolbox's Issues

Add `_.compact` replacement

The types for _.compact seem to work fine. But this is a util that I use frequently, so it would be nice to have a non-underscore version of it.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore: update dependency typescript to v5
  • chore: lock file maintenance
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
  • actions/setup-node v4
  • coverallsapp/github-action v2
  • coverallsapp/github-action v2
npm
package.json
  • @silvermine/chai-strictly-equal 1.1.1
  • @silvermine/eslint-config aa8ecacb12ab778078fd195d3b15f09ecac76c11
  • @silvermine/standardization 2.0.0
  • @silvermine/typescript-config 23213e33077089e723629dead5342abe6f3b3c8c
  • @types/chai 4.1.7
  • @types/mocha 5.2.7
  • @types/node 12.20.45
  • @types/sinon 5.0.7
  • chai 4.2.0
  • check-node-version 4.2.1
  • coveralls 3.1.1
  • eslint 8.57.0
  • grunt 1.6.1
  • grunt-cli 1.3.2
  • grunt-concurrent 3.0.0
  • grunt-contrib-clean 2.0.1
  • grunt-contrib-watch 1.1.0
  • grunt-exec 3.0.0
  • mocha 5.2.0
  • nyc 13.3.0
  • sinon 5.1.1
  • source-map-support 0.5.21
  • ts-node 7.0.1
  • tslib 2.6.3
  • typescript 3.9.10
  • tslib ^1.9.0 || ^2

  • Check this box to trigger a request for Renovate to run again on this repository

Consider adding Promise utility that lets all Promises resolve/reject, but rejects if any are rejected

We would like a Promise utility that acts a little like allSettled in that it allows all Promises in a given array of Promises to resolve or reject, but the end result is a rejected Promise if any Promise in the array is rejected.

This simplest implementation of this would be:

return Promise.allSettled(contentDocPromises).then((results) => {
   for (let result of results) {
      if (result.status !== 'fulfilled') {
         return Promise.reject(result.reason);
      }
   }
});

But that implementation loses the values that resolved Promises returned and only surfaces a single reason for rejection when there could be multiple.

Perhaps the function could resolve with the array of resolved values if all Promises resolve, and reject with an array of rejection reasons for any rejected Promises (or just reject with the array of results so that resolved values are not lost).

Make `isEmpty` a type guard

Currently, isEmpty just returns a boolean. It would be much more useful if it were a type-guard that asserted that its parameter was one of the "empty" object types its checking for.

Proposal: type guard function for the NonNullable<T> type

Scenario

We often need to filter undefined or null values from arrays. The Array#filter method is typed so that if the callback function passed to the filter method is a type guard, filter's return type is the same as what the type guard asserts:

const arr: number | undefined = [ 1, undefined, 2 ];

const onlyNumbers = arr.filter((n): n is number {
   return !isUndefined(n);
});
// onlyNumbers is automatically typed as `number[]`

Instead of writing these type guard functions ad-hoc, it would be convenient if we had a function that asserts that a variable is NonNullable<T>. Then, we could just:

const onlyNumbers = arr.filter(isNotUndefinedOrNull);

Naming

Possible names for the type guard function: isNotUndefinedOrNull, isNonNullable

Make `isEmpty` work for `Set`s

isEmpty doesn't currently work for Sets.

For example:

> let a = new Set([ 1, 2, 3, 4, 5 ]);
> isEmpty(a)
true

I had to hunt around to find the issue in my code and it came back to this. Seems like we have been using sets a bit more recently, so it would be nice to support it in the toolbox.

Add `range` util function

The types for _.range seem fine. But this is a util that I use frequently, so it would be nice to have a non-underscore version of it.

Example:

> _.range(5)
[ 0, 1, 2, 3, 4 ]

Add isBoolean type guard

Toolbox contains type guards for generic types such as isNumber. However, for booleans we have to resort to something like:

interface Animal {
   isAdoptable?: boolean;
}

const stuffedTiger: Animal = {},
      russianBlue: Animal = { isAdoptable: true };

// Type 'undefined' is not assignable to type 'boolean'. (2322)
const isAdoptable: boolean = stuffedTiger.isAdoptable;

if (stuffedTiger.isAdoptable === false || stuffedTiger.isAdoptable === true) {
   // works
   const isAdoptable: boolean = stuffedTiger.isAdoptable;
}

if (stuffedTiger.isAdoptable !== undefined) {
   // works
   const isAdoptable: boolean = stuffedTiger.isAdoptable;
}

TS Playground: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgIImAWzgG2QbwChkTgBnVAEwHsAHMOAIxwgH4AuZR66luEANyEAvoUIJqIMmGTSArjBgRKAFWABzaJ3RZcyALwFhAGmIlzUOWTLB+AIRxyI2jNjyH8yclToNmz5DBLFGEhQgB6cOQVAE9aFAByORBKCBhQZQSvMmQQahk4aw0QJhZA6kC4xO5eCH4EgDpkAAoAJgBmVtaASnFJaWyfelKAmr4QA1kwBSVVDWgG7xph-zDgGBb5RWU1TShFimW-Mv1T5HgcMhQAH2upmZ35-aXfEYMzoKdugjNkSOQAO7UKAAazIvwkUhkLxWLE4YzqE0MW1muwWMOOECEokI602022cz2ByGmOQAEIzslUukQMpvkRzP8gaDweZIQMMSN4TxxpMUY9iVzViIgA

We should add an isBoolean type guard.

Migrate the Undertemplate project to the toolbox

We maintain the Undertemplate project for our minor templating needs. It uses Lodash and makes the templating function CSP-compliant, removing the "eval"-code execution parts of the templating function.

We continue to have a need for Undertemplate, but it would be nice to drop the Lodash dependency and add TypeScript types. We could do so in the Undertemplate project itself, but now that we have the toolbox project that's installed in so many places, it's most convenient to maintain it here. Additionally, the Undertemplate implementation makes use of toolbox functions, so it's helpful to co-locate them.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.