0.2.0 • Published 8 months ago

cast-unknown v0.2.0

Weekly downloads
4
License
MIT
Repository
github
Last release
8 months ago

Cast Unknown

build status npm package

Cast unknown value to desired type with typescript support.

Throws CastError when value is invalid.

Current supported cast target:

  • string
  • number
  • object
  • array
  • date
  • duration (need moment package installed)
  • milliseconds (need moment package installed)
  • promise
  • iterable
  • one (one and the only one item from given iterable, otherwise undefined)
  • nonNull (not null or undefined)
import {
  castString,
  castNumber,
  castObject,
  castArray,
  castDate,
  castPromise,
  castIterable,
  castSoleItem,
  castNonNull,
  CastError,
} from 'cast-unknown';

castString(1);
// '1'
castNumber('2');
// 2
castObject(3);
// <throws CastError>
castArray(4);
// [4]
castArray([5]);
// [5]
castArray(null);
// []
castArray(undefined);
// []
castArray([null]);
// [null]
castArray([undefined]);
// [undefined]
castDate('2019-01-01 12:00');
// <same as `new Date('2019-01-01 12:00')`, but throws CastError if value invalid>
castPromise(6);
// async () => 6
castPromise(() => 7);
// async () => 7
castPromise(async () => 8);
// async () => 8
castIterable(9);
// [9]
const generator = (function* () {
  for (let i; i < 10; i++) {
    yield i;
  }
})();
castIterable(generator);
// generator
castIterable(null);
// []
castIterable(undefined);
// []
castOne(11);
// 11
castOne([12, 13]);
// undefined
castOne([14]);
// 14
castNonNull(15);
// 15
castNonNull(null);
// <throws CastError>
castNonNull(undefined);
// <throws CastError>

related

0.2.0

8 months ago

0.1.9

2 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago