0.0.1 • Published 5 years ago

amida v0.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

amida

github npm:version typescript ci:status document:typedoc license browserslist

Usage

/**
 * As to prepare of using the `amida`
 * 
 * ```bash
 * yarn add amida 
 * ```
 */
import Amida from 'amida';

or

<script src="https://unpkg.com/amida/amida.js"></script>
<script>
  // Can use the `Amida` here.
</script>

Example

interface BehaviorObject {
  size: string;
}

const theme = {
  size: 123,
};

const wm = new WeakMap();
wm.set(theme, theme);

const amida = new Amida<BehaviorObject>({
  size: into => [
    // In case of `string`
    into((v: string) => v).if.string(),

    // In case of `number`
    into((num: number) =>
      Math.round(num) === num ? `${num}px` : `${num}em`,
    ).if.number() /* or if('number') */,

    // In case of `object`
    into((obj: BehaviorObject) => `${obj.size}px`).if.object(),

    // In case of `WeakMap`
    into((wm: WeakMap<object, BehaviorObject>) => {
      return `${wm.get(theme)!.size}px`;
    }).if.weakmap() /* or if ('weakmap') */,
  ],
});

const obj = amida.from(theme);
expect(obj.size).toBe('123px');

obj.size = '100%';
expect(obj.size).toBe('100%');

(obj.size as unknown) = theme;
expect(obj.size).toBe('123px');

(obj.size as unknown) = 999;
expect(obj.size).toBe('999px');

(obj.size as unknown) = 2.1;
expect(obj.size).toBe('2.1em');

(obj.size as unknown) = wm;
expect(obj.size).toBe('123px');

const amida2 = new Amida<BehaviorObject>({
  size: into => [
    into((v: string) => v).if.string(),
    into((v: object) => v).if.object(),
  ]
});

amida2.from({});
(amida2.size as any) = 123; // throw TypeError('size must be string, object')