UNPKG

@kitmi/utils

Version:

A JavaScript utility library for both server and browser

39 lines (38 loc) 1.06 kB
/** * Split a string into two parts by the last occurance of a separator * @param {String} str * @param {String} separator * @returns {Array} [ String, String ] */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { splitFirst: function() { return splitFirst; }, splitLast: function() { return splitLast; } }); const splitLast = (str, separator)=>{ const lastIndex = str.lastIndexOf(separator); return [ lastIndex === -1 ? null : str.substring(0, lastIndex), lastIndex === -1 ? str : str.substring(lastIndex + separator.length) ]; }; const splitFirst = (str, separator)=>{ const index = str.indexOf(separator); return [ index === -1 ? str : str.substring(0, index), index === -1 ? null : str.substring(index + separator.length) ]; }; //# sourceMappingURL=split.js.map