UNPKG

awesome-string

Version:

The ultimate JavaScript string library

30 lines (29 loc) 1.06 kB
import coerceToNumber from 'helper/number/coerce_to_number'; import coerceToString from 'helper/string/coerce_to_string'; /** * Inserts into `subject` a string `toInsert` at specified `position`. * * @function insert * @static * @since 1.0.0 * @memberOf Manipulate * @param {string} [subject=''] The string where to insert. * @param {string} [toInsert=''] The string to be inserted. * @param {number} [position=0] The position to insert. * @return {string} Returns the string after insertion. * @example * as.insert('ct', 'a', 1); * // => 'cat' * * as.insert('sunny', ' day', 5); * // => 'sunny day' */ export default function insert(subject, toInsert, position) { const subjectString = coerceToString(subject); const toInsertString = coerceToString(toInsert); const positionNumber = coerceToNumber(position); if (positionNumber < 0 || positionNumber > subjectString.length || toInsertString === '') { return subjectString; } return subjectString.slice(0, positionNumber) + toInsertString + subjectString.slice(positionNumber); }