Skip to content

A very strange tool for describing tokens, how to use it's only to you!

Notifications You must be signed in to change notification settings

RubaXa/tokenism

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tokenism

A very strange tool for describing tokens, how to use it's only to you!

npm i --save tokenism

How to use

import { createToken } from 'tokenism';

const token = createToken('token', 'Any token', () => Math.random().toString());

token.name();    // "token"
token.caption(); // "Any token"

token.value(); // "0.31454363456"
token.value(); // "0.12476585686"
token.value(); // "0.68209487562"

token.toJSON();
// {
//    name: "token",
//    caption: "Any token",
//    value: "0.68209487562",
//    optional: false,
// }

// Shorty
const tok = token.as('tok');
tok.name();    // "tok"
tok.caption(); // "Any token"

// Shorty with caption
const shortyTok = tok.as(null, 'Shorty token');
shortyTok.name();    // "tok"
shortyTok.caption(); // "Shorty token"

Composite token

import { createToken, composeTokens } from 'tokenism';

const unread = createToken('unread', 'Unread flag', () => Math.random() > .5);
const pinned = createToken('pinnd', 'Pinned flag', () => Math.random() > .5);
const flags = createToken('flags', 'Flags object', composeTokens(
	unread,
	pinned,
));

flags.value();
// {
//   unread: true,
//   pinned: false,
// }

flags.toJSON();
// {
//    name: "flags",
//    caption: "Flags object",
//    optional: false,
//    value: {
//	     unread: {name: "unread", ...},
//	     pinned: {name: "pinned", ...},
//    },
// }

API

  • createToken(name: string, caption: string, generator: () => T): Token
  • composeTokens(...tokens: Token[]): CompositeTokenValue

Token

Instance method

  • as(newName: string | null, newCaption?: string | null, newValue?: T | (() => T)): Token
  • optional(newCaption?: string | null, newValue?: T | (() => T)): Token
  • name(): string
  • caption(): string
  • value(): T
  • toJSON(): object

Development

About

A very strange tool for describing tokens, how to use it's only to you!

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published