1. MyString
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
class MyString { public static readonly charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' public static generateCharset = (privateKey: string, charset?: string): string => { let tempString = charset || this.charset let result = '' for (let i = 0; i < this.charset.length; i += 1) { const kIndex = i % privateKey.length const charCode = privateKey.charCodeAt(kIndex) const tIndex = charCode % tempString.length result = tempString[tIndex] + result tempString = tempString.substring(0, tIndex) + tempString.substring(tIndex + 1) } return result } public static randomString = (length: number, charset?: string) => { const characters = charset || this.charset let result = '' for (let i = 0; i < length; i += 1) { result += characters.charAt(Math.floor(Math.random() * characters.length)) } return result } public static encript = (rootString: string, privateKey: string): string => { const hash = this.generateCharset(privateKey) let result = '' for (let i = 0; i < rootString.length; i += 1) { const index = this.charset.indexOf(rootString[i]) if (index === -1) { result += rootString[i] } else { result += hash[index] } } return result } public static decript = (cipherText: string, privateKey: string): string => { const hash = this.generateCharset(privateKey) let result = '' for (let i = 0; i < cipherText.length; i += 1) { const index = hash.indexOf(cipherText[i]) if (index === -1) { result += cipherText[i] } else { result += this.charset[index] } } return result } } |
2. Date Time
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
export default class MyDatetime { public static timeToText = (time: Date, pattern: string) => { const rules = { YYYY: `${time.getFullYear()}`, YY: `${time.getFullYear()}`.slice(-2), MM: `0${time.getMonth() + 1}`.slice(-2), DD: `0${time.getDate()}`.slice(-2), hh: `0${time.getHours()}`.slice(-2), mm: `0${time.getMinutes()}`.slice(-2), ss: `0${time.getSeconds()}`.slice(-2), } let text = pattern Object.entries(rules).forEach(([key, value]) => { text = text.replace(key, value) }) return text } public static textToTime = (text: string, pattern: string) => { const iFullYear = pattern.indexOf('YYYY') const iMonth = pattern.indexOf('MM') const iDay = pattern.indexOf('DD') const iHours = pattern.indexOf('hh') const iMinutes = pattern.indexOf('mm') const iSeconds = pattern.indexOf('ss') const iMs = pattern.indexOf('xxx') const year = iFullYear !== -1 ? Number(text.slice(iFullYear, iFullYear + 4)) : 0 const month = iMonth !== -1 ? Number(text.slice(iMonth, iMonth + 2)) : 0 const date = iDay !== -1 ? Number(text.slice(iDay, iDay + 2)) : 0 const hours = iHours !== -1 ? Number(text.slice(iHours, iHours + 2)) : 0 const minutes = iMinutes !== -1 ? Number(text.slice(iMinutes, iMinutes + 2)) : 0 const seconds = iSeconds !== -1 ? Number(text.slice(iSeconds, iSeconds + 2)) : 0 const milliseconds = iMs !== -1 ? Number(text.slice(iMs, iMs + 3)) : 0 const time = new Date() time.setFullYear(year) time.setMonth(month - 1) time.setDate(date) time.setHours(hours) time.setMinutes(minutes) time.setSeconds(seconds) time.setMilliseconds(milliseconds) return time } } |
3. Khác
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
class MyObject { static checkDirectExists(object, direct) { let arr = direct.split("."); let dir = "object"; for (let i = 0; i < arr.length; i++) { dir = dir + "." + arr[i]; if (eval('!' + dir)) { return false; } } return true; } static searchValueExists(object, value) { let find = false; let searchObj = (object, value) => { if (find) return; if (Object.prototype.toString.call(object) == "[object Object]") { for (let key in object) { searchObj(object[key], value) } } else { let val = object.toString().toUpperCase(); if (val.indexOf(value.toUpperCase()) > -1) { find = true; } } } searchObj(object, value); return find; } static getValueByDirect(object, direct) { if (typeof direct == "string") { if (MyObject.checkDirectExists(object, direct)) { return eval('object.' + direct); } } else if (typeof direct == "function") { return direct(object); } return; } static setValueByDirect(object, direct, value) { let strToArr = (str) => { //quy định: [5]: array, ["5"]: object let arr = str.split(/\[|\]|\./); let rs = arr.reduce((acc, item) => { if (/^[0-9]+$/.test(item)) item = Number(item); if (item !== "") acc.push(item); return acc; }, []) return rs; } let addProps = (obj, arr, val) => { if (arr.length > 1) { if (typeof arr[1] == 'string') { obj[arr[0]] = obj[arr[0]] || {}; } if (typeof arr[1] == 'number') { obj[arr[0]] = obj[arr[0]] || []; } let temp = obj[arr[0]]; arr.shift(); addProps(temp, arr, val); } else { obj[arr[0]] = val; } return obj; } if (typeof direct == 'string') direct = strToArr(direct); if (Array.isArray(direct)) { return addProps(object, direct, value); } } static cleanUndefined(object) { object = JSON.parse(JSON.stringify(object)); if (Array.isArray(object)) { let array = object.reduce((acc, item) => { if (item) { item = MyObject.cleanUndefined(item) acc.push(item); } return acc; }, []) return array; } if (Object.prototype.toString.call(object) == "[object Object]") { for (let key in object) { object[key] = MyObject.cleanUndefined(object[key]) } return object; } else { return object; } } } class MyForm { static getData(elm) { let data = {}, ipString = elm.querySelectorAll('input[type=text],input[type=password],select'), ipNumber = elm.querySelectorAll('input[type=number]'), ipDate = elm.querySelectorAll('input[type=date]'); ipString.forEach((item, index) => { MyObject.setValueByDirect(data, item.name, item.value) }) ipNumber.forEach((item, index) => { MyObject.setValueByDirect(data, item.name, Number(item.value)) }) ipDate.forEach((item, index) => { if (item.value == "") return; let temp = (new Date(item.value)).getTime(); MyObject.setValueByDirect(data, item.name, temp) }) data = MyObject.cleanUndefined(data); return data; } static setData(object, elm) { elm.reset(); let ipString = elm.querySelectorAll('input[type=text],input[type=password],select'), ipNumber = elm.querySelectorAll('input[type=number]'), ipDate = elm.querySelectorAll('input[type=date]'), ipRadio = elm.querySelectorAll('input[type=radio]'), ipCheckbox = elm.querySelectorAll('input[type=checkbox]'); ipString.forEach((item, index) => { if (item.name == "") return; item.value = MyObject.getValueByDirect(object, item.name); }) ipNumber.forEach((item, index) => { if (item.name == "") return; let temp = MyObject.getValueByDirect(object, item.name); if (!temp) return; if (item.getAttribute("data-money") == "vnd") { item.value = temp.toFixed(3) } else { item.value = temp } }) ipDate.forEach((item, index) => { if (item.name == "") return; let temp = MyObject.getValueByDirect(object, item.name); if (!temp) return; item.value = MyDateTime.format(temp, "YYYY-MM-DD"); }) ipRadio.forEach((item, index) => { if (item.name == "") return; if (item.value == MyObject.getValueByDirect(object, item.name)) { item.checked = true; } }) ipCheckbox.forEach((item, index) => { if (item.name == "") return; if (MyObject.getValueByDirect(object, item.name).includes(item.value)) { item.checked = true; } }) } static autoCompleteMoney(ipNumElement) { ipNumElement.value = Number(ipNumElement.value).toFixed(3); } } class MyNodeList { static groupByAttribute(nodelist, attr) { return Object.values(nodelist).reduce((acc, value) => { let key = value.getAttribute('data-id'); (acc[key] = acc[key] || []).push(value); return acc; }, {}) } } |