hsu-utils 0.0.10 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -25,4 +25,6 @@ yarn add hsu-utils
25
25
  > `number`、`string`、`boolean`、`object`、`array`、`null`、`undefined`、`unknown`、`function`、`symbol`、`date`、`formdata`
26
26
 
27
27
  - `get_string_width` 获取字符串长度
28
- > 中文字符计算为`1`,英文字符计算为`0.5`
28
+ > 根据字体属性进行计算,默认 size 为 12,family 为 微软雅黑
29
+ - `loadImage` 异步加载图片
30
+ > 确保相同的图片只加载一次
package/dist/hsu-utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  *
3
- * hsu-utils v0.0.9
3
+ * hsu-utils v0.0.11
4
4
  *
5
5
  * some front-end utils
6
6
  *
@@ -95,7 +95,17 @@ eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexpo
95
95
  \************************************/
96
96
  /***/ ((__unused_webpack_module, exports) => {
97
97
 
98
- eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nfunction get_string_width(str, font) {\n if (font === void 0) { font = {}; }\n var _a = font.style, style = _a === void 0 ? 'normal' : _a, _b = font.variant, variant = _b === void 0 ? 'normal' : _b, _c = font.weight, weight = _c === void 0 ? 'normal' : _c, _d = font.size, size = _d === void 0 ? 12 : _d, _e = font.lineHeight, lineHeight = _e === void 0 ? 1 : _e, _f = font.family, fontFamily = _f === void 0 ? '微软雅黑, \"Microsoft YaHei\", -apple-system' : _f;\n var canvas = document.createElement('canvas');\n var ctx = canvas.getContext('2d');\n ctx.font = \"\".concat(style, \" \").concat(variant, \" \").concat(weight, \" \").concat(size, \"px/\").concat(lineHeight, \" \").concat(fontFamily);\n var metrics = ctx.measureText(str);\n var width = +(+metrics.width.toFixed(2) + 0.01).toFixed(2);\n return width;\n}\nexports[\"default\"] = get_string_width;\n\n\n//# sourceURL=webpack://hsu-utils/./tools/GetStrWidth/index.ts?");
98
+ eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nfunction get_string_width(str, font) {\n if (font === void 0) { font = {}; }\n var _a = font.style, style = _a === void 0 ? 'normal' : _a, _b = font.variant, variant = _b === void 0 ? 'normal' : _b, _c = font.weight, weight = _c === void 0 ? 'normal' : _c, _d = font.size, size = _d === void 0 ? 12 : _d, _e = font.lineHeight, lineHeight = _e === void 0 ? 1 : _e, _f = font.family, fontFamily = _f === void 0 ? '微软雅黑' : _f;\n var canvas = document.createElement('canvas');\n var ctx = canvas.getContext('2d');\n ctx.font = \"\".concat(style, \" \").concat(variant, \" \").concat(weight, \" \").concat(size, \"px/\").concat(lineHeight, \" \").concat(fontFamily);\n var metrics = ctx.measureText(str);\n var width = +(+metrics.width.toFixed(2) + 0.01).toFixed(2);\n return width;\n}\nexports[\"default\"] = get_string_width;\n\n\n//# sourceURL=webpack://hsu-utils/./tools/GetStrWidth/index.ts?");
99
+
100
+ /***/ }),
101
+
102
+ /***/ "./tools/LoadImage/index.ts":
103
+ /*!**********************************!*\
104
+ !*** ./tools/LoadImage/index.ts ***!
105
+ \**********************************/
106
+ /***/ (function(__unused_webpack_module, exports) {
107
+
108
+ eval("\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nvar __generator = (this && this.__generator) || function (thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [op[0] & 2, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nvar imagePromiseCache = {};\nvar imageCache = {};\nfunction loadImage(url) {\n return __awaiter(this, void 0, void 0, function () {\n var imagePromise;\n return __generator(this, function (_a) {\n if (imageCache[url])\n return [2, imageCache[url]];\n if (imagePromiseCache[url])\n return [2, imagePromiseCache[url]];\n imagePromise = new Promise(function (resolve, reject) {\n var image = new Image();\n image.src = url;\n image.onload = function () {\n resolve(image);\n imageCache[url] = image;\n };\n image.onerror = function () {\n reject();\n };\n });\n imagePromiseCache[url] = imagePromise;\n return [2, imagePromise];\n });\n });\n}\nexports[\"default\"] = loadImage;\n\n\n//# sourceURL=webpack://hsu-utils/./tools/LoadImage/index.ts?");
99
109
 
100
110
  /***/ }),
101
111
 
@@ -115,7 +125,7 @@ eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nfunc
115
125
  \************************/
116
126
  /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
117
127
 
118
- eval("\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.ConvertNumbers = exports.get_string_width = exports.Typeof = exports.Equal = exports.deepCopy = exports.console_table = void 0;\nvar ConsoleTable_1 = __importDefault(__webpack_require__(/*! ./ConsoleTable */ \"./tools/ConsoleTable/index.ts\"));\nexports.console_table = ConsoleTable_1.default;\nvar DeepCopy_1 = __importDefault(__webpack_require__(/*! ./DeepCopy */ \"./tools/DeepCopy/index.ts\"));\nexports.deepCopy = DeepCopy_1.default;\nvar Equal_1 = __importDefault(__webpack_require__(/*! ./Equal */ \"./tools/Equal/index.ts\"));\nexports.Equal = Equal_1.default;\nvar Typeof_1 = __importDefault(__webpack_require__(/*! ./Typeof */ \"./tools/Typeof/index.ts\"));\nexports.Typeof = Typeof_1.default;\nvar GetStrWidth_1 = __importDefault(__webpack_require__(/*! ./GetStrWidth */ \"./tools/GetStrWidth/index.ts\"));\nexports.get_string_width = GetStrWidth_1.default;\nvar ConvertNumbers_1 = __importDefault(__webpack_require__(/*! ./ConvertNumbers */ \"./tools/ConvertNumbers/index.ts\"));\nexports.ConvertNumbers = ConvertNumbers_1.default;\n\n\n//# sourceURL=webpack://hsu-utils/./tools/index.ts?");
128
+ eval("\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.loadImage = exports.ConvertNumbers = exports.get_string_width = exports.Typeof = exports.Equal = exports.deepCopy = exports.console_table = void 0;\nvar ConsoleTable_1 = __importDefault(__webpack_require__(/*! ./ConsoleTable */ \"./tools/ConsoleTable/index.ts\"));\nexports.console_table = ConsoleTable_1.default;\nvar DeepCopy_1 = __importDefault(__webpack_require__(/*! ./DeepCopy */ \"./tools/DeepCopy/index.ts\"));\nexports.deepCopy = DeepCopy_1.default;\nvar Equal_1 = __importDefault(__webpack_require__(/*! ./Equal */ \"./tools/Equal/index.ts\"));\nexports.Equal = Equal_1.default;\nvar Typeof_1 = __importDefault(__webpack_require__(/*! ./Typeof */ \"./tools/Typeof/index.ts\"));\nexports.Typeof = Typeof_1.default;\nvar GetStrWidth_1 = __importDefault(__webpack_require__(/*! ./GetStrWidth */ \"./tools/GetStrWidth/index.ts\"));\nexports.get_string_width = GetStrWidth_1.default;\nvar ConvertNumbers_1 = __importDefault(__webpack_require__(/*! ./ConvertNumbers */ \"./tools/ConvertNumbers/index.ts\"));\nexports.ConvertNumbers = ConvertNumbers_1.default;\nvar LoadImage_1 = __importDefault(__webpack_require__(/*! ./LoadImage */ \"./tools/LoadImage/index.ts\"));\nexports.loadImage = LoadImage_1.default;\n\n\n//# sourceURL=webpack://hsu-utils/./tools/index.ts?");
119
129
 
120
130
  /***/ })
121
131
 
@@ -1,2 +1,2 @@
1
1
  /*! For license information please see hsu-utils.min.js.LICENSE.txt */
2
- !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["hsu-utils"]=t():e["hsu-utils"]=t()}(this,(()=>(()=>{"use strict";var e={442:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0});var r="+",o="-",n="|",a=" ",u="";function i(e){for(var t=0,r=0,o=e;r<o.length;r++){var n=o[r];t+=n.charCodeAt(0)<128&&n.charCodeAt(0)>=0?1:2}return t}function f(e,t){void 0===t&&(t=!1);var n=e.length,a="";return e.forEach((function(e,i){a+=r+function(e){for(var t="",r=0;r<=e;r++)t+=o;return t}(e+1),a+=i===n-1?r+(t?u:"\n"):u})),a}function l(e){for(var t="",r=0;r<=e;r++)t+=a;return t}t.default=function(e,t){var r=e.length,o=function(e){for(var t=Object.keys(e[0]).map((function(e){return+e})),r=0,o=e;r<o.length;r++)o[r].forEach((function(e,r){var o=i(e.toString());t[r]=Math.max(o,t[r])}));return t}(e),c="";e.forEach((function(e,t){t<=1&&(c+=f(o)),c+=function(e,t){var r=e.length,o="";return e.forEach((function(e,f){var c=t[f],s="";isNaN(+c)?s+=a+(null!=c?c:"")+l(e-i((null!=c?c:"").toString())):s+=l(e-i(c.toString()))+c+a,o+=n+s,o+=f===r-1?n+"\n":u})),o}(o,e),t===r-1&&(c+=f(o,!0))})),console.log(c),t&&t(c)}},84:(e,t)=>{function r(e,t){void 0===t&&(t={});var r,o,n=t.benchmark,a=t.textType,u=void 0===a?"lowercase":a,i={lowercase:["零","一","二","三","四","五","六","七","八","九"],uppercase:["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"]},f={lowercase:["","十","百","千"],uppercase:["","拾","佰","仟"]},l={lowercase:["","万","亿","兆"],uppercase:["","万","亿","兆"]},c="";if(n&&(e=+(e*=n).toFixed(4)),e>1e16)return"超出转换范围";var s=e.toString();if(r=s.split(".")[0],o=s.split(".")[1],r)for(var d=0,p=0;p<r.length;p++){var v=r.length-p-1,y=+r[p],h=Math.floor(v/4),b=v%(4*(0===h?1:h));0==+r[p]?d++:(d>0&&(c+=i[u][0],d=0),c+="".concat(i[u][y]+f[u][b]),v%4==0&&(c+=l[u][h]))}if(o){c+="点";for(p=0;p<o.length;p++){var g=+o[p];0==+o[p]?c+="".concat(i[u][0]):c+="".concat(i[u][g])}}return""===c&&(c+=i[u][0]),c}Object.defineProperty(t,"__esModule",{value:!0}),t.toChineseNum=void 0,t.toChineseNum=r;var o={toChineseNum:r};t.default=o},819:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0});var o=r(243);t.default=function e(t){if("date"===(0,o.Typeof)(t))return new Date(t.toISOString());if("formdata"===(0,o.Typeof)(t)){var r=t,n=new FormData;return r.forEach((function(e,t){n.append(t,e)})),n}if(Array.isArray(t))return t.map((function(t){return"object"==typeof t?e(t):t}));if(t&&"object"===(0,o.Typeof)(t)){var a=t,u={};return Object.keys(a).forEach((function(t){var r=a[t];u[t]="object"==typeof r?e(r):r})),u}return t}},962:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.ObjEqual=t.ValEqual=t.TypeEqual=void 0;var o=r(243);function n(e,t){return(0,o.Typeof)(e)===(0,o.Typeof)(t)}function a(e,t){return null===e&&void 0===t||void 0===e&&null===t||("object"==typeof e&&"object"==typeof t?u(e,t):"function"==typeof e&&"function"==typeof t?JSON.stringify(e)===JSON.stringify(t):e===t)}function u(e,t){var r=!1,n=(0,o.deepCopy)(e),u=(0,o.deepCopy)(t);if(null!==n&&null!==u){if(Array.isArray(n)&&Array.isArray(u))if(n.sort(),u.sort(),n.length!==u.length)r=!1;else{r=!0;for(var i=0;i<n.length&&(r=a(n[i],u[i]));i++);}else if("object"==typeof n&&"object"==typeof u)if(Object.keys(n).length!==Object.keys(u).length)r=!1;else for(var f in r=!0,n)if(!(r=a(n[f],u[f])))break}else null===n&&null===u&&(r=!0);return r}t.TypeEqual=n,t.ValEqual=a,t.ObjEqual=u;var i={ValEqual:a,TypeEqual:n,ObjEqual:u};t.default=i},285:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t){void 0===t&&(t={});var r=t.style,o=void 0===r?"normal":r,n=t.variant,a=void 0===n?"normal":n,u=t.weight,i=void 0===u?"normal":u,f=t.size,l=void 0===f?12:f,c=t.lineHeight,s=void 0===c?1:c,d=t.family,p=void 0===d?'微软雅黑, "Microsoft YaHei", -apple-system':d,v=document.createElement("canvas").getContext("2d");return v.font="".concat(o," ").concat(a," ").concat(i," ").concat(l,"px/").concat(s," ").concat(p),+(+v.measureText(e).width.toFixed(2)+.01).toFixed(2)}},332:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e){var t=Object.prototype.toString.call(e),r=new RegExp(" (.+?)]");return t.match(r)[1].toLowerCase()}},243:function(e,t,r){var o=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.ConvertNumbers=t.get_string_width=t.Typeof=t.Equal=t.deepCopy=t.console_table=void 0;var n=o(r(442));t.console_table=n.default;var a=o(r(819));t.deepCopy=a.default;var u=o(r(962));t.Equal=u.default;var i=o(r(332));t.Typeof=i.default;var f=o(r(285));t.get_string_width=f.default;var l=o(r(84));t.ConvertNumbers=l.default}},t={};var r=function r(o){var n=t[o];if(void 0!==n)return n.exports;var a=t[o]={exports:{}};return e[o].call(a.exports,a,a.exports,r),a.exports}(243);return r})()));
2
+ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["hsu-utils"]=t():e["hsu-utils"]=t()}(this,(()=>(()=>{"use strict";var e={442:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0});var r="+",n="-",o="|",a=" ",u="";function i(e){for(var t=0,r=0,n=e;r<n.length;r++){var o=n[r];t+=o.charCodeAt(0)<128&&o.charCodeAt(0)>=0?1:2}return t}function l(e,t){void 0===t&&(t=!1);var o=e.length,a="";return e.forEach((function(e,i){a+=r+function(e){for(var t="",r=0;r<=e;r++)t+=n;return t}(e+1),a+=i===o-1?r+(t?u:"\n"):u})),a}function f(e){for(var t="",r=0;r<=e;r++)t+=a;return t}t.default=function(e,t){var r=e.length,n=function(e){for(var t=Object.keys(e[0]).map((function(e){return+e})),r=0,n=e;r<n.length;r++)n[r].forEach((function(e,r){var n=i(e.toString());t[r]=Math.max(n,t[r])}));return t}(e),c="";e.forEach((function(e,t){t<=1&&(c+=l(n)),c+=function(e,t){var r=e.length,n="";return e.forEach((function(e,l){var c=t[l],s="";isNaN(+c)?s+=a+(null!=c?c:"")+f(e-i((null!=c?c:"").toString())):s+=f(e-i(c.toString()))+c+a,n+=o+s,n+=l===r-1?o+"\n":u})),n}(n,e),t===r-1&&(c+=l(n,!0))})),console.log(c),t&&t(c)}},84:(e,t)=>{function r(e,t){void 0===t&&(t={});var r,n,o=t.benchmark,a=t.textType,u=void 0===a?"lowercase":a,i={lowercase:["零","一","二","三","四","五","六","七","八","九"],uppercase:["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"]},l={lowercase:["","十","百","千"],uppercase:["","拾","佰","仟"]},f={lowercase:["","万","亿","兆"],uppercase:["","万","亿","兆"]},c="";if(o&&(e=+(e*=o).toFixed(4)),e>1e16)return"超出转换范围";var s=e.toString();if(r=s.split(".")[0],n=s.split(".")[1],r)for(var d=0,p=0;p<r.length;p++){var v=r.length-p-1,y=+r[p],h=Math.floor(v/4),b=v%(4*(0===h?1:h));0==+r[p]?d++:(d>0&&(c+=i[u][0],d=0),c+="".concat(i[u][y]+l[u][b]),v%4==0&&(c+=f[u][h]))}if(n){c+="点";for(p=0;p<n.length;p++){var g=+n[p];0==+n[p]?c+="".concat(i[u][0]):c+="".concat(i[u][g])}}return""===c&&(c+=i[u][0]),c}Object.defineProperty(t,"__esModule",{value:!0}),t.toChineseNum=void 0,t.toChineseNum=r;var n={toChineseNum:r};t.default=n},819:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0});var n=r(243);t.default=function e(t){if("date"===(0,n.Typeof)(t))return new Date(t.toISOString());if("formdata"===(0,n.Typeof)(t)){var r=t,o=new FormData;return r.forEach((function(e,t){o.append(t,e)})),o}if(Array.isArray(t))return t.map((function(t){return"object"==typeof t?e(t):t}));if(t&&"object"===(0,n.Typeof)(t)){var a=t,u={};return Object.keys(a).forEach((function(t){var r=a[t];u[t]="object"==typeof r?e(r):r})),u}return t}},962:(e,t,r)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.ObjEqual=t.ValEqual=t.TypeEqual=void 0;var n=r(243);function o(e,t){return(0,n.Typeof)(e)===(0,n.Typeof)(t)}function a(e,t){return null===e&&void 0===t||void 0===e&&null===t||("object"==typeof e&&"object"==typeof t?u(e,t):"function"==typeof e&&"function"==typeof t?JSON.stringify(e)===JSON.stringify(t):e===t)}function u(e,t){var r=!1,o=(0,n.deepCopy)(e),u=(0,n.deepCopy)(t);if(null!==o&&null!==u){if(Array.isArray(o)&&Array.isArray(u))if(o.sort(),u.sort(),o.length!==u.length)r=!1;else{r=!0;for(var i=0;i<o.length&&(r=a(o[i],u[i]));i++);}else if("object"==typeof o&&"object"==typeof u)if(Object.keys(o).length!==Object.keys(u).length)r=!1;else for(var l in r=!0,o)if(!(r=a(o[l],u[l])))break}else null===o&&null===u&&(r=!0);return r}t.TypeEqual=o,t.ValEqual=a,t.ObjEqual=u;var i={ValEqual:a,TypeEqual:o,ObjEqual:u};t.default=i},285:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t){void 0===t&&(t={});var r=t.style,n=void 0===r?"normal":r,o=t.variant,a=void 0===o?"normal":o,u=t.weight,i=void 0===u?"normal":u,l=t.size,f=void 0===l?12:l,c=t.lineHeight,s=void 0===c?1:c,d=t.family,p=void 0===d?"微软雅黑":d,v=document.createElement("canvas").getContext("2d");return v.font="".concat(n," ").concat(a," ").concat(i," ").concat(f,"px/").concat(s," ").concat(p),+(+v.measureText(e).width.toFixed(2)+.01).toFixed(2)}},676:function(e,t){var r=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,a){function u(e){try{l(n.next(e))}catch(e){a(e)}}function i(e){try{l(n.throw(e))}catch(e){a(e)}}function l(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(u,i)}l((n=n.apply(e,t||[])).next())}))},n=this&&this.__generator||function(e,t){var r,n,o,a,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:i(0),throw:i(1),return:i(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function i(i){return function(l){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a&&(a=0,i[0]&&(u=0)),u;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,n=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=u.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=t.call(e,u)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,l])}}};Object.defineProperty(t,"__esModule",{value:!0});var o={},a={};t.default=function(e){return r(this,void 0,void 0,(function(){var t;return n(this,(function(r){return a[e]?[2,a[e]]:o[e]?[2,o[e]]:(t=new Promise((function(t,r){var n=new Image;n.src=e,n.onload=function(){t(n),a[e]=n},n.onerror=function(){r()}})),o[e]=t,[2,t])}))}))}},332:(e,t)=>{Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e){var t=Object.prototype.toString.call(e),r=new RegExp(" (.+?)]");return t.match(r)[1].toLowerCase()}},243:function(e,t,r){var n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(t,"__esModule",{value:!0}),t.loadImage=t.ConvertNumbers=t.get_string_width=t.Typeof=t.Equal=t.deepCopy=t.console_table=void 0;var o=n(r(442));t.console_table=o.default;var a=n(r(819));t.deepCopy=a.default;var u=n(r(962));t.Equal=u.default;var i=n(r(332));t.Typeof=i.default;var l=n(r(285));t.get_string_width=l.default;var f=n(r(84));t.ConvertNumbers=f.default;var c=n(r(676));t.loadImage=c.default}},t={};var r=function r(n){var o=t[n];if(void 0!==o)return o.exports;var a=t[n]={exports:{}};return e[n].call(a.exports,a,a.exports,r),a.exports}(243);return r})()));
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  *
3
- * hsu-utils v0.0.9
3
+ * hsu-utils v0.0.11
4
4
  *
5
5
  * some front-end utils
6
6
  *
@@ -1,5 +1,5 @@
1
1
  export default function get_string_width(str, font = {}) {
2
- const { style = 'normal', variant = 'normal', weight = 'normal', size = 12, lineHeight = 1, family: fontFamily = '微软雅黑, "Microsoft YaHei", -apple-system' } = font;
2
+ const { style = 'normal', variant = 'normal', weight = 'normal', size = 12, lineHeight = 1, family: fontFamily = '微软雅黑' } = font;
3
3
  const canvas = document.createElement('canvas');
4
4
  const ctx = canvas.getContext('2d');
5
5
  ctx.font = `${style} ${variant} ${weight} ${size}px/${lineHeight} ${fontFamily}`;
@@ -0,0 +1 @@
1
+ export default function loadImage(url: string): Promise<HTMLImageElement>;
@@ -0,0 +1,32 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ const imagePromiseCache = {};
11
+ const imageCache = {};
12
+ export default function loadImage(url) {
13
+ return __awaiter(this, void 0, void 0, function* () {
14
+ if (imageCache[url])
15
+ return imageCache[url];
16
+ if (imagePromiseCache[url])
17
+ return imagePromiseCache[url];
18
+ const imagePromise = new Promise((resolve, reject) => {
19
+ const image = new Image();
20
+ image.src = url;
21
+ image.onload = () => {
22
+ resolve(image);
23
+ imageCache[url] = image;
24
+ };
25
+ image.onerror = () => {
26
+ reject();
27
+ };
28
+ });
29
+ imagePromiseCache[url] = imagePromise;
30
+ return imagePromise;
31
+ });
32
+ }
package/es/index.d.ts CHANGED
@@ -4,6 +4,7 @@ import Equal from './Equal';
4
4
  import Typeof from './Typeof';
5
5
  import get_string_width from './GetStrWidth';
6
6
  import ConvertNumbers from './ConvertNumbers';
7
- export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers };
7
+ import loadImage from './LoadImage';
8
+ export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage };
8
9
  import { ConsoleData } from './ConsoleTable';
9
10
  export type { ConsoleData };
package/es/index.js CHANGED
@@ -4,4 +4,5 @@ import Equal from './Equal';
4
4
  import Typeof from './Typeof';
5
5
  import get_string_width from './GetStrWidth';
6
6
  import ConvertNumbers from './ConvertNumbers';
7
- export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers };
7
+ import loadImage from './LoadImage';
8
+ export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage };
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  function get_string_width(str, font) {
4
4
  if (font === void 0) { font = {}; }
5
- var _a = font.style, style = _a === void 0 ? 'normal' : _a, _b = font.variant, variant = _b === void 0 ? 'normal' : _b, _c = font.weight, weight = _c === void 0 ? 'normal' : _c, _d = font.size, size = _d === void 0 ? 12 : _d, _e = font.lineHeight, lineHeight = _e === void 0 ? 1 : _e, _f = font.family, fontFamily = _f === void 0 ? '微软雅黑, "Microsoft YaHei", -apple-system' : _f;
5
+ var _a = font.style, style = _a === void 0 ? 'normal' : _a, _b = font.variant, variant = _b === void 0 ? 'normal' : _b, _c = font.weight, weight = _c === void 0 ? 'normal' : _c, _d = font.size, size = _d === void 0 ? 12 : _d, _e = font.lineHeight, lineHeight = _e === void 0 ? 1 : _e, _f = font.family, fontFamily = _f === void 0 ? '微软雅黑' : _f;
6
6
  var canvas = document.createElement('canvas');
7
7
  var ctx = canvas.getContext('2d');
8
8
  ctx.font = "".concat(style, " ").concat(variant, " ").concat(weight, " ").concat(size, "px/").concat(lineHeight, " ").concat(fontFamily);
@@ -0,0 +1 @@
1
+ export default function loadImage(url: string): Promise<HTMLImageElement>;
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ var imagePromiseCache = {};
40
+ var imageCache = {};
41
+ function loadImage(url) {
42
+ return __awaiter(this, void 0, void 0, function () {
43
+ var imagePromise;
44
+ return __generator(this, function (_a) {
45
+ if (imageCache[url])
46
+ return [2, imageCache[url]];
47
+ if (imagePromiseCache[url])
48
+ return [2, imagePromiseCache[url]];
49
+ imagePromise = new Promise(function (resolve, reject) {
50
+ var image = new Image();
51
+ image.src = url;
52
+ image.onload = function () {
53
+ resolve(image);
54
+ imageCache[url] = image;
55
+ };
56
+ image.onerror = function () {
57
+ reject();
58
+ };
59
+ });
60
+ imagePromiseCache[url] = imagePromise;
61
+ return [2, imagePromise];
62
+ });
63
+ });
64
+ }
65
+ exports.default = loadImage;
package/lib/index.d.ts CHANGED
@@ -4,6 +4,7 @@ import Equal from './Equal';
4
4
  import Typeof from './Typeof';
5
5
  import get_string_width from './GetStrWidth';
6
6
  import ConvertNumbers from './ConvertNumbers';
7
- export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers };
7
+ import loadImage from './LoadImage';
8
+ export { console_table, deepCopy, Equal, Typeof, get_string_width, ConvertNumbers, loadImage };
8
9
  import { ConsoleData } from './ConsoleTable';
9
10
  export type { ConsoleData };
package/lib/index.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.ConvertNumbers = exports.get_string_width = exports.Typeof = exports.Equal = exports.deepCopy = exports.console_table = void 0;
6
+ exports.loadImage = exports.ConvertNumbers = exports.get_string_width = exports.Typeof = exports.Equal = exports.deepCopy = exports.console_table = void 0;
7
7
  var ConsoleTable_1 = __importDefault(require("./ConsoleTable"));
8
8
  exports.console_table = ConsoleTable_1.default;
9
9
  var DeepCopy_1 = __importDefault(require("./DeepCopy"));
@@ -16,3 +16,5 @@ var GetStrWidth_1 = __importDefault(require("./GetStrWidth"));
16
16
  exports.get_string_width = GetStrWidth_1.default;
17
17
  var ConvertNumbers_1 = __importDefault(require("./ConvertNumbers"));
18
18
  exports.ConvertNumbers = ConvertNumbers_1.default;
19
+ var LoadImage_1 = __importDefault(require("./LoadImage"));
20
+ exports.loadImage = LoadImage_1.default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hsu-utils",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "description": "some front-end utils",
5
5
  "repository": "git@github.com:VitaTsui/hsu-utils.git",
6
6
  "author": "VitaHsu <vitahsu7@gmail.com>",