@lcap/asl
Version:
NetEase Application Specific Language
62 lines • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sliceTagContent = exports.getLastOfPath = exports.unique = exports.firstLowerCase = exports.firstUpperCase = exports.Camel2kebab = exports.kebab2Camel = void 0;
/**
* 中划线格式 -转-> 驼峰格式
* @param name 原名称
* @return 转换后的名称
*/
const kebab2Camel = (name) => name.replace(/(?:^|-)([a-zA-Z0-9])/g, (m, $1) => $1.toUpperCase());
exports.kebab2Camel = kebab2Camel;
/**
* 驼峰格式 -转-> 中划线格式
* @param name 原名称
* @return 转换后的名称
*/
const Camel2kebab = (name) => name.replace(/([A-Z]|[0-9]+)/g, (m, $1, offset) => (offset ? '-' : '') + $1.toLowerCase());
exports.Camel2kebab = Camel2kebab;
/**
* 首字母大写
* @param value 字符串
*/
const firstUpperCase = (value) => value.replace(/^\S/, (letter) => letter.toUpperCase());
exports.firstUpperCase = firstUpperCase;
const firstLowerCase = (value) => value.replace(/^\S/, (letter) => letter.toLowerCase());
exports.firstLowerCase = firstLowerCase;
/**
* @param key 键
* @param set 数据集合,Array | Map | Set
* @param start 数字起始
*/
function unique(key, set, start = 1) {
const has = (_key) => {
if (set instanceof Set || set instanceof Map)
return set.has(_key);
else if (Array.isArray(set))
return set.includes(_key);
else
return set[_key];
};
while (has(key))
key = key.replace(/\d*$/, (m) => String(m === '' ? start : +m + 1));
return key;
}
exports.unique = unique;
const getLastOfPath = (path) => {
const arr = path.split('/');
return arr[arr.length - 1];
};
exports.getLastOfPath = getLastOfPath;
/**
* 切出标签内的内容
* @param source 原始内容
* @param tag 标签
* @param attrs 属性匹配
*/
const sliceTagContent = (source, tag, attrs = '') => {
const reg = new RegExp(`<${tag}${attrs ? ' ' + attrs : ''}.*?>([\\s\\S]+)<\\/${tag}>`);
const m = source.match(reg);
return m ? m[1].replace(/^\n+/, '') : '';
};
exports.sliceTagContent = sliceTagContent;
//# sourceMappingURL=string.js.map