nicecode-v2/packages/func/lib/string/index.js

91 lines
2.5 KiB
JavaScript

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/string/index.ts
var string_exports = {};
__export(string_exports, {
cutStr: () => cutStr,
getStrLength: () => getStrLength,
getValueByUrl: () => getValueByUrl,
isUrl: () => isUrl,
pxToRem: () => pxToRem
});
module.exports = __toCommonJS(string_exports);
var getStrLength = function(str) {
var realLength = 0, len = str.length, charCode = -1;
for (var i = 0; i < len; i++) {
charCode = str.charCodeAt(i);
if (charCode >= 0 && charCode <= 128)
realLength += 1;
else
realLength += 2;
}
return realLength;
};
var cutStr = function cutstr(str, len) {
var str_length = 0;
var str_len = 0;
let str_cut = new String();
str_len = str.length;
for (var i = 0; i < str_len; i++) {
let a = str.charAt(i);
str_length++;
if (escape(a).length > 4) {
str_length++;
}
str_cut = str_cut.concat(a);
if (str_length >= len) {
str_cut = str_cut.concat("...");
return str_cut;
}
}
if (str_length < len) {
return str;
}
};
var isUrl = (str) => {
try {
new URL(str);
return true;
} catch (err) {
return false;
}
};
var getValueByUrl = (key, str) => {
let result = null;
if (isUrl(str)) {
result = new URL(str).searchParams.get(key);
} else {
result = new URLSearchParams(str.indexOf("?") > -1 ? str : `?${str}`).get(key);
}
return result;
};
var pxToRem = (value, rootFontSize) => {
const fontSize = rootFontSize || 80;
const valueArr = value.split(" ");
return valueArr.filter((o) => o).map((val) => parseFloat(val) / fontSize + "rem").join(" ");
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
cutStr,
getStrLength,
getValueByUrl,
isUrl,
pxToRem
});