javascript utils
npm install @impactor/javascriptJavascript utils
- It provides tools to work with arrays, objects, strings and other types.
``ts
// add data to the clipboard
copy("hello world!");
// using template interpolation
copy("hello {{name}}", { name: "world" });
`
`ts
// check if a container contains an element
// the container can be a string, object, an array, or a RegExp
includes(["a", "b"], "b"); //true
includes({ a: true, b: true }, "b"); //true
includes("x", "xyz"); //true
includes(/x/, "xyz"); // true
includes(/x/, ["x", "y", "z"]); // true
// divide an array into chunks
chunk(["a", "b", "c", "d", "e"], 2); // [["a", "b"], ["c", "d"], ["e"]]
// filter objects
filterObjectByKeys({ a: 1, b: 2, c: 3 }, ["a", "c"]); // { a: 1, c: 3 }
// convert dot notation style into a real object
dotNotationToObject("a.b.c", "value"); // {a: {b:{ c: "value"}}}
// converts an object-like string into a plain object
parseObject("k1=v1,k2=v2"); // {k1: "v1", k2: "v2"}
parseObject('["value"]'); // {value: true}
parseObject('{k1:"v1", k2:"v2"}'); // {k1: "v1", k2: "v2"}
// rename object key
renameObjectKey({ a: 1, b: 2 }, "a", "x"); // {x:1, b: 2}
renameObjectKeys({ a: 1, b: 2 }, { a: "x", b: "y" }); // {x:1, y: 2}
// remove comments from a json string
let content = readFileSync("./tsconfig.json", "utf8");
let json = cleanJson(content);
`
`ts`
replace(["a", "b", "c"], "b", "x"); // ["a", "x", "c"]
`ts`
request("https://api.example.com/test", { status: "ok" });
- As the HTTP method not specified, it will considered as POST if the body is not empty, otherwise it will be considered as GET.res.json()
- You don't need to stringify the JSON body.
- It returns the JSON response directly, if the response content type is json (the default), you don't need to use
The function is a thin wrapper around the built-in fetch() to make the requests too much easier.
`ts
// convert a string into a regexp
toRegExp(".+(\d)", "i");
// merge patterns
mergePatterns(/x/, /y/); // /xy/
mergePatterns(/x/, /y/, { delimiter: "|", flags: "i" }); // /x|y/i
`
also, there are many ready to use patterns, such as ip, ipv6, email, phone, url, domain, link, strongPassword, hashtag, ...
- toCamelCase(): convert kabab-case strings into camelCasetoKababCase()
- toUpperCaseFirst()
- : capitalize the first lettercleanString()
- : remove comments, break lines and trim the string from multi-line stringsreplaceAsync()
- and replaceRecursive()
`tsconnected in ${duration / 1000} seconds
// measure the execution's duration.
timer("connection");
await connect();
let duration = timer("connection");
console.log();
// pause a function execution
await sleep(1000);
`
`ts
objectType("text"); // string
objectType({x: 1}); // object
objectType([1, 2, 3]); // array
objectType(1); // number
...
isPlainObject({x: 1}); // true
objectType(new Date()); // object
isPlainObject(new Date()); // false
// check if the element is iterable, but not a string.
isIterable([1, 2, 3]); //true
isIterable({x: 1}); //true
// check if the object a promise or a promise-like
isPromise(new Promise(...)); //true
isEmpty(undefined); // true
isEmpty(null); // true
isEmpty(""); // true
isEmpty(" "); // true
isEmpty([]); // true
isEmpty({}); // true
isEmpty(false); // false
isEmpty("0"); // false
`
- queryToObject(): converts query params to a plain objectobjectToQueryParams()`
-