UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

20 lines (19 loc) 795 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.delete_suffix = delete_suffix; function delete_suffix(str, suffix) { // parity verified: Ruby 3.3 // discuss at: https://locutus.io/ruby/String/delete_suffix/ // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Removes suffix when present and otherwise returns the original string unchanged. // example 1: delete_suffix('value-suffix', '-suffix') // returns 1: 'value' // example 2: delete_suffix('value', 'x') // returns 2: 'value' const input = str + ''; const needle = suffix + ''; if (needle === '') { return input; } return input.endsWith(needle) ? input.slice(0, -needle.length) : input; }