🎉 Initial commit

master
KaKi87 1 year ago
commit 5e45f90aab

@ -0,0 +1,44 @@
/**
* @param {String|{string: String, start: Number, deleteCount: Number|undefined, item: String|undefined}} arg
* @param {Number} [start]
* @param {Number} [deleteCount]
* @param {String} [item]
* @returns {String}
*/
const stringSplice = (
arg,
start,
deleteCount,
item
) => {
const
_string = (typeof arg === 'string' ? arg : arg.string).split(''),
_start = arg.start || start,
_deleteCount = arg.deleteCount || deleteCount,
_item = arg.item || item;
_string.splice(_start, _deleteCount, _item);
return _string.join('');
};
/**
* @param {String} string
* @param {{start: Number, deleteCount: Number|undefined, item: String|undefined}[]} items
* @returns {String}
*/
const stringSpliceMulti = (string, items) => {
let n = 0;
for(
const { start, deleteCount, item }
of items.sort((a, b) => a.start - b.start)
){
string = stringSplice(string, start + n, deleteCount, item);
n += item?.length || 0;
n -= deleteCount || 0;
}
return string;
};
export {
stringSplice,
stringSpliceMulti
};

@ -0,0 +1,9 @@
{
"name": "strsplice",
"version": "0.1.0",
"main": "mod.js",
"repository": "ssh://kaki@git.kaki87.net:3021/KaKi87/strsplice.git",
"author": "KaKi87 <KaKi87@pm.me>",
"license": "MIT",
"type": "module"
}
Loading…
Cancel
Save