awesome-string
Version:
The ultimate JavaScript string library
28 lines (27 loc) • 941 B
JavaScript
import coerceToString from 'helper/string/coerce_to_string';
/**
* Extracts from `subject` a string from `start` position up to `end` position. The character at `end` position is not
* included.
*
* @function slice
* @static
* @since 1.0.0
* @memberOf Chop
* @param {string} [subject=''] The string to extract from.
* @param {number} start The position to start extraction. If negative use `subject.length + start`.
* @param {number} [end=subject.length] The position to end extraction. If negative use `subject.length + end`.
* @return {string} Returns the extracted string.
* @note Uses native `String.prototype.slice()`
* @example
* as.slice('miami', 1);
* // => 'iami'
*
* as.slice('florida', -4);
* // => 'rida'
*
* as.slice('florida', 1, 4);
* // => "lor"
*/
export default function slice(subject, start, end) {
return coerceToString(subject).slice(start, end);
}