UNPKG

voca

Version:

The ultimate JavaScript string library

38 lines (32 loc) 1.14 kB
import './internal/is_nil.js'; import './is_string.js'; import { c as coerceToString } from './internal/coerce_to_string.js'; import { c as coerceToNumber } from './internal/coerce_to_number.js'; /** * 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 * v.insert('ct', 'a', 1); * // => 'cat' * * v.insert('sunny', ' day', 5); * // => 'sunny day' */ function insert(subject, toInsert, position) { var subjectString = coerceToString(subject); var toInsertString = coerceToString(toInsert); var positionNumber = coerceToNumber(position); if (positionNumber < 0 || positionNumber > subjectString.length || toInsertString === '') { return subjectString; } return subjectString.slice(0, positionNumber) + toInsertString + subjectString.slice(positionNumber); } export default insert;