@starryn/util
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

@starryn/util

a base util library

features

  • typings & typings guards
  • string operation
  • math operation
  • tree operation
  • hash algorithm
  • binary encoding & decoding
  • collections
  • functions
  • style relevants
  • dom relevants

demos

some basic usage for some modules

typings
import {Action, Action2, Func, Func2, Predicate, Tuple, Tuple2} from '@starryn/util'

const action: Action<string> = (arg: string): void => console.log(arg);
const action2: Action2<string, number> = (arg1: string, arg2: number): void => console.log(arg1, arg2);


const func: Func<string, boolean> = (arg: string): boolean => !!arg;
const func2: Func<string, number, boolean> = (arg1: string, arg2: number): boolean => !!(arg1 + arg2);

const predicate: Predicate<number> = (arg: number): boolean => arg === 5;

const tuple: Tuple<number> = [2];
const tuple2: Tuple2<number, string> = [2, 'hello world'];
string operation
import { pad, padRight, kebablize, pascalize, camelize } from '@starryn/util'

console.log(pad('12', 4))                 // 0012
console.log(pad('12', 4, 'nn'))           // nn12

console.log(padRight('12', 4))            // 1200
console.log(padRight('12', 4, 'nn'))      // 12nn

console.log(kebablize('HelloWorld'))      // hello-world
console.log(pascalize('hello-world'))     // HelloWorld
console.log(camelize('HelloWorld'))       // helloWorld
console.log(camelize('hello-world'))      // helloWorld
math operation
import { clamp, toFixedEx } from '@starryn/util'

console.log(clamp(-1, 0, 100))                            // 0
console.log(clamp(101, 0, 100))                           // 100
console.log(clamp(2, 0, 100))                             // 2

console.log(toFixedEx(99.9999,2), 99.9999.toFixed(2))     // 99.99  100.00
hash algorithm
import { murmurHash3, Murmur3 } from '@starryn/util';

(async()=>{
  const str = 'hello world';
  
  const hash1 = murmurHash3(str);                 // use javascript float number
  const hash2 = await Murmur3.hash32Async(str);   // use strickly int32 number

  console.log(hash1, hash2, hash1 === hash2);
})();
style relevant
import { createRoot } from '@starryn/util';

const [bem] = createRoot('o');

console.log(bem('btn'))       // o-btn
console.log(bem('btn',{active: true, hide: false}))   // o-btn o-btn--active
console.log(bem('btn', ['long']))                     // o-btn o-btn--long
cossole.log(bem('btn', ['long',{active: true}]))      // o-btn o-btn--long o-btn--active
functions
import { debounce, throttle } from '@starryn/util';

const dfunc = debounce(()=>console.log(1), 200)         //  ship from lodash
const tfunc = throttle(() => console.log(2), 300)       // ship from lodash

Package Sidebar

Install

npm i @starryn/util

Weekly Downloads

6

Version

1.2.0

License

MIT

Unpacked Size

643 kB

Total Files

72

Last publish

Collaborators

  • starry_night