Skip to content

Instantly share code, notes, and snippets.

@nexpr
Last active March 20, 2020 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nexpr/0c77f428073201923fcc4ad111b04274 to your computer and use it in GitHub Desktop.
Save nexpr/0c77f428073201923fcc4ad111b04274 to your computer and use it in GitHub Desktop.

Usage

Install

npm install gist:0c77f428073201923fcc4ad111b04274

Run (Node.js 12)

node --experimental-modules index.mjs

Example

import * as utils from "jscoreutils"

console.log(utils.htmlEscape("<div>"))
// &lt;div&gt;

console.log(utils.dateFormat("%Y/%m/%d %H:%M:%S"))
// 2020/03/19 01:48:27

console.log(utils.stringSplit("a/b/c/d/e", "/", 3))
// [ 'a', 'b', 'c/d/e' ]

console.log(utils.arraySortBy([1, 18, 61, 10, 128, 41, 20], x => x % 10))
// [10, 20, 1, 61, 41, 18, 128]
export const regexpEscape = str => {
return str.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&")
}
export const htmlEscape = str => {
return str.replace(/[&<>"]/g, c => {
switch (c) {
case "&": return "&amp;"
case "<": return "&lt;"
case ">": return "&gt;"
case "\x22": return "&quot;"
}
})
}
export const htmlUnescape = str => {
return str.replace(/&amp;|&lt;|&gt;|&quot;/g, m => {
switch (m) {
case "&amp;": return "&"
case "&lt;": return "<"
case "&gt;": return ">"
case "&quot;": return "\x22"
}
})
}
export const arrayShuffle = array => {
for (let i = 0, l = array.length; i < l; i++) {
const j = ~~(Math.random() * l)
const t = array[i]
array[i] = array[j]
array[j] = t
}
return array
}
export const arraySortBy = (array, ...bys) => {
const compare = (a, b) => (a === b ? 0 : a < b ? -1 : 1)
return array.sort((a, b) => {
if (bys.length === 0) {
return compare(a, b)
}
for (const by of bys) {
const result = typeof by === "function" ? compare(by(a), by(b)) : compare(a[by], b[by])
if (result) return result
}
return 0
})
}
export const stringSplit = (str, by, len) => {
len = ~~len
if (len <= 0) return []
if (by === "") {
const result = []
let j = 0
for (let i = 0; i < len - 1; i++) {
const code = str.codePointAt(i)
if (code === undefined) {
break
}
result.push(String.fromCodePoint(code))
j += code > 65535 ? 2 : 1
}
if (str.length > j) {
result.push(str.slice(j))
}
return result
} else {
const result = []
let pre_index = 0
for (let i = 0; i < len - 1; i++) {
const index = str.indexOf(by, pre_index)
if (index === -1) {
break
}
result.push(str.slice(pre_index, index))
pre_index = index + by.length
}
result.push(str.slice(pre_index))
return result
}
}
export const dateFormat = (format, date) => {
date = date == null ? new Date() : new Date(date)
return String(format).replace(/%./g, m => {
switch (m[1]) {
case "d": return String(date.getDate()).padStart(2, "0")
case "H": return String(date.getHours()).padStart(2, "0")
case "I": return String(date.getHours() % 12).padStart(2, "0")
case "m": return String(date.getMonth() + 1).padStart(2, "0")
case "M": return String(date.getMinutes()).padStart(2, "0")
case "p": return date.getHours() >= 12 ? "PM" : "AM"
case "s": return String(date.getMilliseconds()).padStart(3, "0")
case "S": return String(date.getSeconds()).padStart(2, "0")
case "w": return String(date.getDay())
case "y": return String(date.getFullYear()).slice(2)
case "Y": return String(date.getFullYear())
default:
return m[1]
}
})
}
export const jsonEquals = (a, b) => {
const format = x => {
if (x && typeof x === "object") {
if (Array.isArray(x)) {
return x.map(format)
} else {
const entries = Object.entries(x)
.sort((a, b) => a[0].localeCompare(b[0]))
.map(e => [e[0], format(e[1])])
return { entries }
}
} else {
return x
}
}
return JSON.stringify(format(a)) === JSON.stringify(format(b))
}
{
"name": "jscoreutils",
"version": "1.0.0",
"type": "module",
"main": "./index.js"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment