npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dabapps/simple-records

v0.1.8

Published

Simple Readonly TypeScript Records

Downloads

23

Readme

Simple Records

Simple Readonly TypeScript Records

Build Status

This module provides a number of helper functions for working with TypeScript ReadOnly objects.

Disclaimer

This module is in its early stages and until it reaches its first major version it may be unstable, with potentially breaking changes with new minor version releases.

Patch version changes will include both minor changes and patches.

Installation

Install via NPM:

npm install @dabapps/simple-records --save

If you are using a version of npm that doesn't support package lock files, we'd recommend installing with the --save-exact flag to pin to a specific version in your package.json.

Available interfaces

Dict

Dict is a simple type alias for a Readonly Object with arbitrary string keys.

const x: Dict<string> = {
  arbitrary: 'data',
};

JSON and ReadonlyJSON

When dealing with unknown recursive data shapes from a server, you can specify them as JSON or ReadonlyJSON;

const x: JSON = {
  hi: {
    there: [
      1, 2, 3, 'a'
    ]
  }
}

SimpleRecord

SimpleRecord creates a function that produces a given Readonly interface, with default values for keys that aren't provided.

type IMyRecord = Readonly<{
  a: string;
  b: number;
}>;

const MyRecord = SimpleRecord<IMyRecord>({
  a: 'default string for a',
  b: 0
});

const myInstance = MyRecord({
  b: 5
});

// myInstance == {
//   a: 'default string for a',
//   b: 5
// }

RecordWithConstructor

RecordWithConstructor takes an input interface of raw data, an output interface of completed data, a set of defaults for the input, and a callback function that must translate between the two types. This is used for when you have complex types (such as nested records, or Moment objects).

type IMyRecordInput = Readonly<{
  a: string;
  b: string | Date;
}>;

type IMyRecord = Readonly<{
  a: string;
  b: moment.Moment;
}>;

const MyRecord = RecordWithConstructor<IMyRecordInput, IMyRecord>({
  a: '',
  b: new Date(),
}, (input) => {
  return {
    ...input,
    b: moment.utc(input.b)
  };
});

const myInstance = MyRecord({b: '1970-01-01'});
// myInstance.b is now a Moment object

Proplist

A proplist is an array of 2-tuples of shape [string, T], often used to represent simple ordered dictionary types and cases where mutiple keys may exist for the same item. They can be converted to Dicts, but duplicate keys beyond the first will be discarded and ordering will be lost.

const myProplist: Proplist<number> = [['a', 1], ['b', 2], ['b', 3]];

const myDict = proplistToDict(myProplist);
// myDict == {
//   a: 1,
//   b: 2
// }

const myOtherProplist = dictToProplist({a: 1, b: 2});
// myOtherProplist == [
//   ['a', 1],
//   ['b', 2],
// ];
// // Order is not guaranteed as Dict is unordered.

There is also a MutableProplist type, for times when you need mutability.

OrderedDict

An OrderedDict is an object that combines the guaranteed order characteristics of an Array with the indexability of a Dict. They can be created from Dicts (although, bare in mind Dicts are not ordered so you will need to provide a sort, otherwise it will sort alphabetically by key), and from Proplists (which will cause duplicate values to be dropped). They can also be created from scratch. It is not recommended as it is error-prone, but you can also initialize them with a list of keys, and a dictionary of items.

const orderedDict1 = new OrderedDict<number>();
const orderedDict2 = OrderedDict.fromDict({'a': 1, 'b': 2, 'c': 3});
const orderedDict3 = OrderedDict.fromProplist([['a', 1], ['b', 2], ['c', 3]]);

// Don't do this unless you're really sure
const handInitialized = new OrderedDict<number>(['a', 'b', 'c'], {'a': 1, 'b': 2, 'c': 3});

const orderedDictUpdated = orderedDict2.set('b', 5);
const valueOfB = orderedDictUpdated.get('b');

const orderedDictMerged = orderedDict2.merge(orderedDict3, ([leftKey, leftValue], [rightKey, rightValue]) => leftValue - rightValue);

Additional Array-like methods are provided to match the behaviour of native Arrays.

Code of conduct

For guidelines regarding the code of conduct when contributing to this repository please review https://www.dabapps.com/open-source/code-of-conduct/