JSPM

  • Created
  • Published
  • Downloads 15
  • Score
    100M100P100Q45821F
  • License MIT

Native TypeScript containers for generic values. Reliably store and retrieve values without writing validation or type checking code. Use predefined types & validators, or make your own.

Package Exports

  • @toreda/strong-types

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@toreda/strong-types) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@toreda/strong-types

CI Coverage Quality Gate Status

Native TypeScript containers for generic value storage. Reliably store and retrieve typed values without writing validation or type checking code. Use built-in types or define your own.

What does it do?

import {StrongInt, makeInt} from '@toreda/strong-types';
//  int with initial value 10.
const int = makeInt(10, 0);
// Prints 10. It always return an int.
console.log(int());

// Set the value
int(11);
// Prints 11.
console.log(int());

// Won't set the value - it's not an int.
int(null);
int(undefined);
int(3.33);
int({});

// Prints 11
console.log(int());

Contents

Basic Usage

Set value

// Set value to string 'trendy'
myValue('trendy');

Set value to null

// Set value to be null.
myValue(null);

Reset

Reset will set value to null without using set functionality.

// set kvp to hello
myValue('hello');

// Outputs hello
myValue();

// value will become null.
myValue.reset();

Supported Types

StrongArray

StrongBoolean

StrongDouble

StrongInt

Instantiation

import {StrongInt, makeInt} from '@toreda/strong-types';
const initial = 11;
const fallback = 55;
const int = makeInt(initial, fallback);

// Returns 11 - initial value was 11.
const value = StrongInt();

Default Fallback

import {StrongInt, makeInt} from '@toreda/strong-types';
const initial = null;
const fallback = 101;
const uint = makeInt(initial, fallback);

// Returns 101 - current value is null (no value set).
const value = uint();

Fallback

import {StrongInt, makeInt} from '@toreda/strong-types';
const int = makeInt(null, 201);

// kvp.get(fallback) returns the fallback argument when the kvp instance
// has no value set.
const fallback = 11;
// Returns 11 - value is currently null.
const value = int.get(fallback);

// Set value to 500.
int(500);
// value is set to 500, because value is now a valid int with value 100.
const value = int.get(fallback);

Validation

StrongInt will not update t's value called with a positive or negative integer.

import {StrongInt, makeInt} from '@toreda/strong-types';
const uint = makeInt(50, 100);

// Attempting to set value to a negative integer.
// Success will be false.
const success = uint(1.5);

// value is still 50. 1.5 is not an integer.
const value = uint();
Individual Fallbacks
import {StrongUInt, makeUInt} from '@toreda/strong-types';
const uint = makeUInt(null, 30);

// kvp.get(fallback) returns the fallback when the kvp
// has no value set.
const fallback = 25;
// Returns 25 because value is currently null.
const value = uint.get(fallback);

// Set value to 100.
uint(100);
// value is set to 100, because value is now a valid int with value 100.
const value = uint.get(fallback);
Type Validation

StrongUInt performs automatic input validation and will not update it's value unless the provided input is an unsigned integer.

import {StrongUInt, makeUInt} from '@toreda/strong-types';
const uint = makeUInt(20, 40);

// Attempting to set value to a negative integer.
// Success will be false.
const success = uint(-10);

// value is still initial value 20 because -10 above is not an
// unsigned integer. No errors on thrown on invalid input.
const value = uint();

Create value

import {StrongType, makeStrong} from '@toreda/strong-types';
const initial = 'hello';
const fallbackDefault = 'goodbye';
const myValue = makeStrong<string>(initial, fallbackDefault);

Get value

// Returns current value, or fallback when value is null.
const value = myValue();

Get value with custom fallback

// value set to 'hello' as shown above.
const fallback = 'goodbye again';
// Returns value if set, or fallback otherwise.
const value = myValue.get(fallback);

StrongString

StrongUInt

Accepts positive integer values only. Everything else will be rejected and will not update the value.

Instantiation
import {StrongUInt, makeUInt} from '@toreda/strong-types';
// UInt starting value.
const initial = 44;
const fallbackDefault = 1;
const uint = makeUInt(initialValue, fallbackDefault);

// Get the current value 44.
const uintValue = uint();

// Set value to 14.
uint(14);
Using the Fallback Default
import {StrongUInt, makeUInt} from '@toreda/strong-types';
const initialValue = null;
const fallbackDefault = 27;
const uint = makeUInt(initialValue, fallbackDefault);

// Returns 27. Getting the current value with uint() guarantees a type-safe return value.
// When the current value is null (not set), the default fallback is returned instead.
const value = uint();

Install

Install @toreda/strong-types directly from NPM or clone the Github repo.

Install using Yarn (preferred)

  1. Open a shell (or console).
  2. Navigate to the the StrongTypes root project folder.
  3. Enter the following commands in order. Wait for each to complete before typing the next.
yarn

Install using NPM

  1. Open a shell (or console).
  2. Navigate to the the StrongTypes root project folder.
  3. Enter the following commands in order. Wait for each to complete before typing the next.
npm install

Run Unit Tests

Install or clone StrongTypes (see above).

StrongTypes tests are written with Jest which is also a project dev dependency.

Installing jest is not required after project dependencies are installed (see above).

yarn test

Build from source

The next steps are the same whether you installed the package using NPM or cloned the repo from Github.

Build with Yarn

Enter the following commands in order from the StrongTypes root project folder.

yarn build

Build with NPM

Enter the following commands in order from the StrongTypes root project folder.

npm run-script build

License

MIT © Toreda, Inc.