@newdash/newdash
Version:
javascript/typescript utility library
60 lines (59 loc) • 2.09 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.split = void 0;
const castSlice_1 = __importDefault(require("./.internal/castSlice"));
const hasUnicode_1 = __importDefault(require("./.internal/hasUnicode"));
const isRegExp_1 = __importDefault(require("./isRegExp"));
const stringToArray_1 = __importDefault(require("./.internal/stringToArray"));
const isIterateeCall_1 = __importDefault(require("./.internal/isIterateeCall"));
const toString_1 = __importDefault(require("./toString"));
const baseToString_1 = __importDefault(require("./.internal/baseToString"));
/**
* Used as references for the maximum length and index of an array.
* @ignore
* @internal
* @private
*/
const MAX_ARRAY_LENGTH = 4294967295;
/**
* Splits `string` by `separator`.
*
* **Note:** This method is based on
* [`String#split`](https://mdn.io/String/split).
*
* @since 5.0.0
* @category String
* @param str The string to split.
* @param separator The separator pattern to split by.
* @param limit The length to truncate results to.
* @returns Returns the string segments.
* @example
*
* ```js
* split('a-b-c', '-', 2)
* // => ['a', 'b']
* ```
*/
function split(str, separator, limit) {
if (limit && typeof limit != "number" && (0, isIterateeCall_1.default)(str, separator, limit)) {
separator = limit = undefined;
}
limit = limit === undefined ? MAX_ARRAY_LENGTH : limit >>> 0;
if (!limit) {
return [];
}
str = (0, toString_1.default)(str);
if (str && (typeof separator == "string" ||
(separator != null && !(0, isRegExp_1.default)(separator)))) {
separator = (0, baseToString_1.default)(separator);
if (!separator && (0, hasUnicode_1.default)(str)) {
return (0, castSlice_1.default)((0, stringToArray_1.default)(str), 0, limit);
}
}
return str.split(separator, limit);
}
exports.split = split;
exports.default = split;