Socket
Socket
Sign inDemoInstall

pre-suf

Package Overview
Dependencies
208
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.4 to 1.1.0

52

lib/index.js
'use strict';
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var escapeString = require('escape-string-regexp');
function makeRegExp(str, leading) {
var repeat = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
var check = function check(str) {
var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'str';
var escaped = escapeString(str);
if (typeof str !== 'string') {
throw new TypeError(name + ' must be a string, but got ' + str);
}
};
if (repeat) {
var make = function make(str, fix, remove, leading) {
check(str);
var name = leading ? 'prefix' : 'suffix';
check(fix, name);
var escaped = escapeString(fix);
if (remove) {
escaped = '(?:' + escaped + ')*';

@@ -20,31 +34,27 @@ }

return new RegExp(escaped);
}
return [str, new RegExp(escaped)];
};
exports.ensureLeading = function (str, prefix) {
var regex = makeRegExp(prefix, true, false);
var remove = function remove(str, regex) {
return str.replace(regex, '');
};
if (regex.test(str)) {
return str;
}
var ensure = function ensure(str, regex, fix, leading) {
return regex.test(str) ? str : leading ? fix + str : str + fix;
};
return prefix + str;
exports.ensureLeading = function (str, prefix) {
return ensure.apply(undefined, _toConsumableArray(make(str, prefix, false, true)).concat([prefix, true]));
};
exports.removeLeading = function (str, prefix) {
return str.replace(makeRegExp(prefix, true), '');
return remove.apply(undefined, _toConsumableArray(make(str, prefix, true, true)));
};
exports.ensureEnding = function (str, suffix) {
var regex = makeRegExp(suffix, false, false);
if (regex.test(str)) {
return str;
}
return str + suffix;
return ensure.apply(undefined, _toConsumableArray(make(str, suffix)).concat([suffix]));
};
exports.removeEnding = function (str, suffix) {
return str.replace(makeRegExp(suffix, false), '');
return remove.apply(undefined, _toConsumableArray(make(str, suffix, true)));
};
{
"name": "pre-suf",
"version": "1.0.4",
"version": "1.1.0",
"description": "Manipulate strings with prefixes and suffixes.",

@@ -37,4 +37,6 @@ "main": "./lib",

"dependencies": {
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"escape-string-regexp": "^1.0.5"
}
}

@@ -1,10 +0,21 @@

'use strict'
const escapeString = require('escape-string-regexp')
const check = (str, name = 'str') => {
if (typeof str !== 'string') {
throw new TypeError(`${name} must be a string, but got ${str}`)
}
}
function makeRegExp (str, leading, repeat = true) {
let escaped = escapeString(str)
const make = (str, fix, remove, leading) => {
check(str)
if (repeat) {
const name = leading
? 'prefix'
: 'suffix'
check(fix, name)
let escaped = escapeString(fix)
if (remove) {
escaped = `(?:${escaped})*`

@@ -19,35 +30,23 @@ }

return new RegExp(escaped)
return [str, new RegExp(escaped)]
}
const remove = (str, regex) => str.replace(regex, '')
exports.ensureLeading = (str, prefix) => {
let regex = makeRegExp(prefix, true, false)
const ensure = (str, regex, fix, leading) => regex.test(str)
? str
: leading
? fix + str
: str + fix
if (regex.test(str)) {
return str
}
exports.ensureLeading = (str, prefix) =>
ensure(...make(str, prefix, false, true), prefix, true)
return prefix + str
}
exports.removeLeading = (str, prefix) =>
remove(...make(str, prefix, true, true))
exports.ensureEnding = (str, suffix) =>
ensure(...make(str, suffix), suffix)
exports.removeLeading = (str, prefix) => {
return str.replace(makeRegExp(prefix, true), '')
}
exports.ensureEnding = (str, suffix) => {
let regex = makeRegExp(suffix, false, false)
if (regex.test(str)) {
return str
}
return str + suffix
}
exports.removeEnding = (str, suffix) => {
return str.replace(makeRegExp(suffix, false), '')
}
exports.removeEnding = (str, suffix) =>
remove(...make(str, suffix, true))

@@ -38,2 +38,47 @@ 'use strict';

})
it('error', function () {
throws(function () {
presuf.ensureLeading()
}, 'str must be a string, but got undefined', 'ensureLeading str')
throws(function () {
presuf.ensureLeading('a')
}, 'prefix must be a string, but got undefined', 'ensureLeading str')
throws(function () {
presuf.removeLeading()
}, 'str must be a string, but got undefined', 'ensureLeading str')
throws(function () {
presuf.removeLeading('a')
}, 'prefix must be a string, but got undefined', 'ensureLeading str')
throws(function () {
presuf.ensureEnding()
}, 'str must be a string, but got undefined', 'ensureLeading str')
throws(function () {
presuf.ensureEnding('a')
}, 'suffix must be a string, but got undefined', 'ensureLeading str')
throws(function () {
presuf.removeEnding()
}, 'str must be a string, but got undefined', 'ensureLeading str')
throws(function () {
presuf.removeEnding('a')
}, 'suffix must be a string, but got undefined', 'ensureLeading str')
})
})
function throws (fn, message, d) {
try {
fn()
} catch (e) {
expect(e.message).to.equal(message)
return
}
throw new Error('should fail: ' + d)
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc