UNPKG

voca

Version:

The ultimate JavaScript string library

39 lines (33 loc) 1.03 kB
import { i as isNil } from './internal/is_nil.js'; import './is_string.js'; import { c as coerceToString } from './internal/coerce_to_string.js'; import { c as clipNumber, M as MAX_SAFE_INTEGER, t as toInteger } from './internal/to_integer.js'; /** * Extracts the first `length` characters from `subject`. * * @function first * @static * @since 1.0.0 * @memberOf Chop * @param {string} [subject=''] The string to extract from. * @param {int} [length=1] The number of characters to extract. * @return {string} Returns the first characters string. * @example * v.first('helicopter'); * // => 'h' * * v.first('vehicle', 2); * // => 've' * * v.first('car', 5); * // => 'car' */ function first(subject, length) { var subjectString = coerceToString(subject); var lengthInt = isNil(length) ? 1 : clipNumber(toInteger(length), 0, MAX_SAFE_INTEGER); if (subjectString.length <= lengthInt) { return subjectString; } return subjectString.substr(0, lengthInt); } export default first;