froebel
Version:
TypeScript utility library
30 lines (25 loc) • 830 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.surround = exports.default = void 0;
var _except = require("./except");
/**
* Surrounds the `str` with `surrounding`. `surrounding` must have an even length.
*
* @example
* ```
* surround("foo", "()") // "(foo)"
* surround("foo", "({[]})") // "({[foo]})"
* ```
*/
const surround = (str, surrounding) => {
if (typeof surrounding !== "string") return str;
(0, _except.assert)(surrounding.length % 2 === 0, "surrounding string must have even length");
const half = surrounding.length / 2;
return `${surrounding.slice(0, half)}${str}${surrounding.slice(half, half * 2)}`;
};
exports.surround = surround;
var _default = surround;
exports.default = _default;
module.exports = Object.assign(exports.default || {}, exports);