@imshann/utils v1.9.4
Introduction
Collection of common function tools
- Introduction
import
import * as _ from "@imshann/utils";timer
const newTimer = _.timer({delay:3000, callback: () => {}})
newTimer.start();
newTimer.stop();deepLoop
_.deepLoop(data, (item) => {
console.log(item);
});toNumber
_.toNumber("123"); // => 123Math
simplifiedFraction
简化分数
_.simplifiedFraction(4, 24); // => { numerator: 1, denominator: 6 }event
事件类
decorator
装饰器
document.addEventListener("click", (event) => {
const dv = _.decorator(event);
});getPosition
获取鼠标事件的 offset 位置
dv.getPosition(); // => {x: 100, y: 100}tree
const data = [
{
id: "da0bb8d9-6d37-4971-98a8-e762d684ba26",
label: "A",
children: [
{
id: "a2f126a5-ce30-4f52-aacf-129d5958de58",
label: "A1",
},
],
},
];flatten
_.tree(data).flatten();
// => [{id: "da0bb8d9-6d37-4971-98a8-e762d684ba26", label: "A"}, {id: "a2f126a5-ce30-4f52-aacf-129d5958de58", label: "A1"}]findById
根据编号查找树的节点
_.tree(data).findById("a2f126a5-ce30-4f52-aacf-129d5958de58");
// => {id: "a2f126a5-ce30-4f52-aacf-129d5958de58", label: "A1"}after
追加元素到指定节点后面
_.tree(data)
.findById("a2f126a5-ce30-4f52-aacf-129d5958de58")
.after({ label: "A2" });
// => [{id: "a2f126a5-ce30-4f52-aacf-129d5958de58", label: "A1"}, {id: "d7f35d7d-93fe-48d4-ab3d-5d37dc0dbcb1", label: "A2"}]before
追加元素到指定节点前面
_.tree(data)
.findById("a2f126a5-ce30-4f52-aacf-129d5958de58")
.after({ label: "A2" });
/*
[
{ id: "d7f35d7d-93fe-48d4-ab3d-5d37dc0dbcb1", label: "A1" },
{ id: "a2f126a5-ce30-4f52-aacf-129d5958de58", label: "A1" },
]
*/append
追加元素到指定节点的子节点列表往后的位置
_.tree(data)
.findById("da0bb8d9-6d37-4971-98a8-e762d684ba26")
.append({ label: "A2" });
// => [{id: "a2f126a5-ce30-4f52-aacf-129d5958de58", label: "A1"}, {id: "d7f35d7d-93fe-48d4-ab3d-5d37dc0dbcb1", label: "A1"}]prepend
往子节点集合的前面追加新节点
_.tree(data)
.findById("da0bb8d9-6d37-4971-98a8-e762d684ba26")
.append({ label: "A2" });
/*
=> [
{id: "d7f35d7d-93fe-48d4-ab3d-5d37dc0dbcb1", label: "A2"},
{id: "a2f126a5-ce30-4f52-aacf-129d5958de58", label: "A1"}
]
*/remove
移除指定节点
_.tree(data).remove("a2f126a5-ce30-4f52-aacf-129d5958de58");
// => []common
clone
对数组进行浅拷贝
_.clone([1, 2]); // => [1, 2]value
获取对象或数组的值
_.value([1, 2], 1); // => 2
_.value([1, 2], 2); // => null
_.value({ a: { b: "c" } }, "a.b"); // => curl
setQueryParam(key: string, value: string): string
_.url("https://example.com/index.html?id=1").setQueryParam({ name: "shann" });
// => https://example.com/index.html?id=1&name=shannguid
_.guid();object
hasValue(value: string | boolean | number): boolean
const obj = { isActive: true };
_.object(obj).hasValue(true);
// => truefilterEmpty(): ObjectExtend
_.object({ a: 1, b: null }).filterEmpty();
// => { a: 1 }each(fn: (value: any, key?: string) => void): void
_.object({ color: "red", backgroundColor: "blue" }).each((value, key) => {
values.push(key + ":" + value);
});
// => ['color:red', 'backgroundColor:blue']assign( newVal: { key: string: any }, defaults: { key: string: any } = {} )
_.object({ a: 1, b: 2 }).assign({ a: 2, c: 3 });
// => { a: 2, b: 2 }extend(target: { index: string: any }): ObjectExtend
_.object({ a: 1, b: 2 }).extend({ a: 2, c: 3 });
// => { a: 2, b: 2, c: 3 }clone()
_.object({ a: 1, b: 2 }).clone();
// => { a: 1, b: 2 }clear(properties: string[]): void
_.object({ a: 1, b: 2 }).clone(["b"]);
// => { a: 1 }number
between
between(range: number[]): boolean
_.number(1).between(0, 10);getTextWidthByElement
getTextWidthByElement(element: HTMLElement): number
_.getTextWidthByElement(document.getElementById("text")); // => 107strategy
const strategy = _.strategy();
strategy.add("case1", () => {
return "I am case1";
});
strategy.add("case2", () => {
return "I am case2";
});
strategy.call("case1"); // => I am case1
strategy.call("case2"); // => I am case2limitMin
limitMin(value: number, least: number): number
_.limitMin(10, 0); // => 10
_.limitMin(-1, 0); // => 0getCharacterWidth
getCharacterWidth(character: stirng, font: string): number
_.getCharacterWidth("A", "bold 12pt arial"); // => 14selection
getText(): string
_.selection().getText();element
append(element: HTMLElement, position: number): void
function getElement(content) {
const div = document.createElement("div");
div.innerHTML = content;
return div;
}
const parent = _.element(document.querySelector("#parent"));
parent.append(getElement("111"));
parent.append(getElement("333"));
parent.append(getElement("222"), 0);
// <div id="parent">
// <div>111</div>
// <div>222</div>
// <div>333</div>
// </div>html
toSourceIndex
// => a b => a b
// => 012 => 01234567
_.html("a b").toSourceIndex(2); // => 7string
toKebabCase
转成横杆命名
_.toKebabCase("MyName"); // => my-nametoUnderScoreCase
转成下划线命名
_.toUnderScoreCase("MyName"); // => my_nameisUnderScoreCase
判断是否下划线命名
_.isUnderScoreCase("my_name"); // => truetoLowerCamelCase
将大驼峰命名转成小驼峰命名
_.toLowerCamelCase("MyName"); // => myName
_.toLowerCamelCase("my_name"); // => myNameucfrist
将英文字符串的首字母转成大写
_.ucfrist("home"); // => Homeappend
_.string("ab").append("c"); // => abcdelete
_.string("abc").delete(1); // => actoHTML
_.string("a b").toHTML(); // => a bProxy
call method at the class outer
class Target {
sayHello() {
console.log("Hello, I am target class.");
}
}
const target = _.ProxyFactory(Target, null, {
beforeMethod() {
console.log("The beforeMethod method has been called.");
},
afterMethod() {
console.log("The afterMethod method has been called.");
},
});
target.sayHello();
// => The beforeMethod method has been called.
// => Hello, I am target class.
// => The afterMethod method has been called.call method in the class
class Target {
initialize() {
this.sayHello();
}
sayHello() {
console.log("Hello, I am target class.");
}
}
const target = _.ProxyFactory(Target, null, {
beforeMethod() {
console.log("The beforeMethod method has been called.");
},
afterMethod() {
console.log("The afterMethod method has been called.");
},
});
// => The beforeMethod method has been called.
// => Hello, I am target class.
// => The afterMethod method has been called.Collection
add(element: any, index?: number): []
_.collection([1]).add(2); // => [1, 2]
_.collection([1]).add([2, 3]); // => [1, 2, 3]
_.collection([1, 3]).add(2, 1); // => [1, 2, 3]
_.collection([1, 4]).add([2, 3], 1); // => [1, 2, 3, 4]rewrite(element: any, start: number): []
_.collection([1]).rewrite(2, 0); // => [2]
_.collection([1, 2]).rewrite([2, 3], 1); // => [1, 2, 3]delele(index: number): []
_.collection([1, 2]).delete(1); // => [1]assign(target: { key: string: any })
_.collection([{ a: 1 }, { a: 2 }]).assign({ b: 3 }); // => [{ a: 1, b: 3}, { a: 2, b: 3}]removeByField(field: string, value: any): void
_.collection([{ a: 1 }, { a: 2 }]).removeByField("a", 1); // => [{ a: 2}]isNotNull
_.isNotNull("foo"); // => true
_.isNotNull(null); // => falseisNull
_.isNull(null); // => true
_.isNull("foo"); // => falseisFunction
_.isFunction(() => {}); // => trueisNumber
_.isNumber(123); // => trueisArray
_.isArray([]); // => trueisString
_.isString("111"); // => trueisUndefined
_.isUndefined(undefined); // => trueisNotUndefined
_.isNotUndefined(undefined); // => falseisBoolean
_.isBoolean(true); // => trueisNotBoolean
_.isNotBoolean(true); // => falseisEmpty
_.isEmpty(null); // => true
_.isEmpty(undefined); // => true
_.isEmpty([]); // => true
_.isEmpty(""); // => true
_.isEmpty(0); // => true
_.isEmpty({}); // => trueisNotEmpty
_.isNotEmpty([1]); // => trueisTelephone
_.isTelephone("13512345678"); // = trueisEmail
_.isEmail("test@example.com"); // => true9 months ago
8 months ago
1 year ago
1 year ago
1 year ago
8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
9 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago