Package Exports
- @om-design/util
- @om-design/util/dist/index.cjs
- @om-design/util/dist/index.mjs
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 (@om-design/util) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@om-design/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 '@om-design/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 '@om-design/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 '@om-design/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 '@om-design/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 '@om-design/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 '@om-design/util';
const dfunc = debounce(()=>console.log(1), 200) // ship from lodash
const tfunc = throttle(() => console.log(2), 300) // ship from lodash