Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

string-utilz

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

string-utilz - npm Package Compare versions

Comparing version 0.5.0 to 1.0.0

__tests__/integration/string-utilz.int.js

5

CHANGELOG.md

@@ -0,2 +1,7 @@

# 2017-02-26 - v1.0.0
- Update test names to more closely match lib names
- Rework [string-utilz.js](lib/string-utilz.js) to optionally add prototypes
- Update all tests to ensure refactor functions as expected
# 2017-02-25 - v0.5.0
- Initial release

339

lib/string-utilz.js

@@ -27,183 +27,190 @@ /**

/* #fmt constants */
const __open = '%{';
const __close = '}';
const __esc = '%';
/**
* Simple method to escape all special RegExp fxns
*
* Special values:
* - [ ] / { } ( ) * + ? . \ ^ $ |
*
* @param {string} tmpStr to escape
* @returns {string} with all special values escaped
*/
const escapeRegEx = (tmpStr) => { return tmpStr.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); }
/**
* Abstract away RegEx management...
*
* @param {string} tmpStr to escape
* @param {string} flags RegEx flags requested
* @return {RegExp} escaped string RegExp with given flags
*
* @see #escapeRegEx(tmpStr)
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Advanced_searching_with_flags
*/
function generateRegEx(tmpStr, flags) {
return new RegExp(tmpStr.escapeRegEx(), flags || '');
}
const generateRegEx = (tmpStr, flags) => { return new RegExp(escapeRegEx(tmpStr), flags || ''); }
if (typeof String.prototype.startsWith != 'function') {
/**
* starts with functionality
*
* @param {string}
* string to match against
* @returns {boolean} <b>true</b> if this ends with string, <b>false</b>
* otherwise
*
* @usage 'bob'.startsWith('b'); => true
* @usage 'A long string'.startsWith('A lon') => true
* @usage 'A long string'.startsWith('A lone') => false
*/
String.prototype.startsWith = function (str) {
return this.slice(0, str.length) == str;
};
}
/**
* Generic replace to make code climate happier (and code more DRY)
*
* @param {string} tmpStr string to do the 'replace old with new' on
* @param {string} oldStr string to replace
* @param {string} newStr string to replace with
* @param {string} flags RegExp flags
* @return {string}
*
* @see #generateRegEx(tmpStr, flags)
* @see #escapeRegEx(tmpStr)
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Advanced_searching_with_flags
*/
const replaceAllGeneric = (tmpStr, oldStr, newStr, flags) => { return tmpStr.replace(generateRegEx(oldStr, flags), newStr); }
if (typeof String.prototype.endsWith != 'function') {
/**
* starts with functionality
*
* @param {string}
* string to match against
* @returns {boolean} <b>true</b> if this starts with string, <b>false</b>
* otherwise
*
* @usage 'bob'.endsWith('b'); => true
* @usage 'A long string'.endsWith('string') => true
* @usage 'A long string'.endsWith('a string') => false
*/
String.prototype.endsWith = function (str) {
return this.slice(-str.length) == str;
};
}
/**
* starts with functionality
*
* @param {string} tmpStr string to check
* @param {string} matchStr string to match against
* @returns {boolean} <b>true</b> if this ends with string, <b>false</b> otherwise
*
* @usage 'bob'.startsWith('b'); => true
* @usage 'A long string'.startsWith('A lon') => true
* @usage 'A long string'.startsWith('A lone') => false
*/
const startsWith = (tmpStr, matchStr) => { return tmpStr.slice(0, matchStr.length) == matchStr; }
if (typeof String.prototype.containsIgnoreCase != 'function') {
/**
* Simple true/false to tell if the given string matches (ignoring case)
* some subset of <b>this</b> string
*
* @param {string}
* to match against (ignoring case)
* @returns {boolean} <b>true</b> if the string is contained (without
* matching case), <b>false</b> otherwise
*
* @usage 'my string'.containsIgnoreCase('str') => true
* @usage 'my long string'.containsIgnoreCase('long') => true
* @usage 'my long string'.containsIgnoreCase('LONG') => true
* @usage 'my super long string'.containsIgnoreCase('rings') => false
*/
String.prototype.containsIgnoreCase = function (str) {
return this.search(generateRegEx(str, 'i')) > -1;
};
}
/**
* ends with functionality
*
* @param {string} tmpStr string to check
* @param {string} matchStr string to match against
* @returns {boolean} <b>true</b> if this ends with string, <b>false</b> otherwise
*
* @usage 'bob'.endsWith('b'); => true
* @usage 'A long string'.endsWith('string') => true
* @usage 'A long string'.endsWith('a string') => false
*/
const endsWith = (tmpStr, matchStr) => { return tmpStr.slice(-matchStr.length) == matchStr; }
/**
* Generic replace to make code climate happier (and code more DRY)
* Simple true/false to tell if the given string matches (ignoring case)
* some subset of <b>this</b> string
*
* @param {string} tmpStr string to check
* @param {string} matchStr string to match against (ignoring case)
* @returns {boolean} <b>true</b> if the string is contained (without matching case), <b>false</b> otherwise
*
* @usage 'my string'.containsIgnoreCase('str') => true
* @usage 'my long string'.containsIgnoreCase('long') => true
* @usage 'my long string'.containsIgnoreCase('LONG') => true
* @usage 'my super long string'.containsIgnoreCase('rings') => false
*/
function replaceAllGeneric(tmpStr, oldStr, newStr, flags) {
return tmpStr.replace(generateRegEx(oldStr, flags), newStr);
}
const containsIgnoreCase = (tmpStr, matchStr) => { return tmpStr.search(generateRegEx(matchStr, 'i')) > -1; }
if (typeof String.prototype.replaceAll != 'function') {
/**
* Replace all functionality
*
* @param {string}
* string to replace
* @param {string}
* string to replace with
* @returns {string} with values replaced
*
* @usage 'bob'.replaceAll('b','m'); => 'mom'
* @usage 'My very long string'.replaceAll(' ','_'); =>
* 'My_very_long_string'
*/
String.prototype.replaceAll = function (oldStr, newStr) {
return replaceAllGeneric(this, oldStr, newStr, 'g');
};
}
/**
* Replace all functionality
*
* @param {string} tmpStr string to do the 'replace old with new' on
* @param {string} oldStr string to replace
* @param {string} newStr string to replace with
* @returns {string} with values replaced
*
* @usage 'bob'.replaceAll('b','m'); => 'mom'
* @usage 'My very long string'.replaceAll(' ','_'); => 'My_very_long_string'
*/
const replaceAll = (tmpStr, oldStr, newStr) => { return replaceAllGeneric(tmpStr, oldStr, newStr, 'g'); }
if (typeof String.prototype.replaceAllIgnoreCase != 'function') {
/**
* Replace all functionality that ignores case
*
* @param {string}
* string to replace
* @param {string}
* string to replace with
* @returns {string} with values replaced
*
* @usage 'Bob'.replaceAll('b','m'); => 'mom'
* @usage 'My very long string'.replaceAll(' ','_'); =>
* 'My_very_long_string'
*/
String.prototype.replaceAllIgnoreCase = function (oldStr, newStr) {
return replaceAllGeneric(this, oldStr, newStr, 'gi');
};
}
/**
* Replace all functionality that ignores case
*
* @param {string} tmpStr string to do the 'replace old with new' on
* @param {string} oldStr string to replace
* @param {string} newStr string to replace with
* @returns {string} with values replaced
*
* @usage 'Bob'.replaceAll('b','m'); => 'mom'
* @usage 'My very long string'.replaceAll(' ','_'); => 'My_very_long_string'
*/
const replaceAllIgnoreCase = (tmpStr, oldStr, newStr) => { return replaceAllGeneric(tmpStr, oldStr, newStr, 'gi'); }
if (typeof String.prototype.escapeRegEx != 'function') {
/**
* Simple method to escape all special RegExp fxns
*
* Special values:
* - [ ] / { } ( ) * + ? . \ ^ $ |
*
* @returns {string} with all special values escaped
*/
String.prototype.escapeRegEx = function () {
return this.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
};
}
/**
* Format functionality for String class
*
* @param {string} first parameter is the format string
* @param {string...} string(s) to replace
* @returns {string} with values replaced
*
* @usage String.fmt('%{s}','bob'); => 'bob'
* @usage String.fmt('My %{s} long %{s}','very', 'string'); => 'My very long string'
* @usage String.fmt('%{0} says %{1}, thanks %{0}!','Bob', 'Hi'); => 'Bob says Hi, thanks Bob!'
*/
const fmt = function () {
var args = Array.from(arguments);
var tmpStr = args.shift(),
argIndex = 0;
// handle simple indicies
tmpStr = tmpStr.replace(/%\{(\d+)\}/g, function (match, group1) {
return args[group1];
});
// handle simple string replacements
tmpStr = tmpStr.replace(/%\{s\}/g, function (match, offset, str) {
if (str.charAt(offset - 1) != __esc) {
return args[argIndex++];
}
return match;
});
return tmpStr;
};
if (typeof String.prototype.fmt != 'function') {
/**
* Format functionality for a string object
*
* @param {string...}
* string(s) to replace
* @returns {string} with values replaced
*
* @usage '%{s}'.fmt('bob'); => 'bob'
* @usage 'My %{s} long %{s}'.fmt('very', 'string'); =>
* 'My very long string'
* @usage '%{0} says %{1}, thanks %{0}!'.fmt('Bob', 'Hi');
* => 'Bob says Hi, thanks Bob!'
*
* @see String.fmt()
*/
String.prototype.fmt = function () {
return String.fmt.apply(null, [this].concat(Array.from(arguments)));
};
}
/**
* Adds the following to the `String.prototype` if it's not already a function:
* - startsWith
* - endsWith
* - containsIgnoreCase
* - replaceAll
* - replaceAllIgnoreCase
* - escapeRegEx
* - fmt
*
* Also sets `String.fmt` = fmt
*
* This is NON-DESTRUCTIVE! If there is already a function defined, no new function will be set.
*/
const addPrototypes = () => {
if (typeof String.prototype.startsWith != 'function')
String.prototype.startsWith = function (matchStr) { return startsWith.call(null, this, matchStr); }
if (typeof String.fmt != 'function') {
/**
* Format functionality for String class
*
* @param {string} first parameter is the format string
* @param {string...}
* string(s) to replace
* @returns {string} with values replaced
*
* @usage String.fmt('%{s}','bob'); => 'bob'
* @usage String.fmt('My %{s} long %{s}','very', 'string'); =>
* 'My very long string'
* @usage String.fmt('%{0} says %{1}, thanks %{0}!','Bob', 'Hi');
* => 'Bob says Hi, thanks Bob!'
*
* @see String.prototype.fmt()
*/
String.fmt = function () {
const __open = '%{';
const __close = '}';
const __esc = '%';
if (typeof String.prototype.endsWith != 'function')
String.prototype.endsWith = function (matchStr) { return endsWith.call(null, this, matchStr); }
var args = Array.from(arguments);
var tmpStr = args.shift(),
argIndex = 0;
// handle simple indicies
tmpStr = tmpStr.replace(/%\{(\d+)\}/g, function (match, group1) {
return args[group1];
});
// handle simple string replacements
tmpStr = tmpStr.replace(/%\{s\}/g, function (match, offset, str) {
if (str.charAt(offset - 1) != __esc) {
return args[argIndex++];
}
return match;
});
return tmpStr;
};
if (typeof String.prototype.containsIgnoreCase != 'function')
String.prototype.containsIgnoreCase = function (matchStr) { return containsIgnoreCase.call(null, this, matchStr); }
if (typeof String.prototype.replaceAll != 'function')
String.prototype.replaceAll = function (oldStr, newStr) { return replaceAll.call(null, this, oldStr, newStr); }
if (typeof String.prototype.replaceAllIgnoreCase != 'function')
String.prototype.replaceAllIgnoreCase = function (oldStr, newStr) { return replaceAllIgnoreCase.call(null, this, oldStr, newStr); }
if (typeof String.prototype.escapeRegEx != 'function')
String.prototype.escapeRegEx = function () { return escapeRegEx.call(null, this); }
if (typeof String.prototype.fmt != 'function')
String.prototype.fmt = function () { return fmt.apply(null, [this].concat(Array.from(arguments))); }
if (typeof String.fmt != 'function')
String.fmt = fmt;
}
module.exports = {
startsWith: startsWith,
endsWith: endsWith,
containsIgnoreCase: containsIgnoreCase,
replaceAll: replaceAll,
replaceAllIgnoreCase: replaceAllIgnoreCase,
escapeRegEx: escapeRegEx,
fmt: fmt,
addStringPrototypes: addPrototypes
}
{
"name": "string-utilz",
"version": "0.5.0",
"version": "1.0.0",
"description": "String utilities for JavaScript.",

@@ -5,0 +5,0 @@ "main": "lib/string-utilz.js",

@@ -22,5 +22,16 @@ # String Utilz

- Install [the npm](https://www.npmjs.com/package/string-utilz) in your project: `npm install --save string-utilz`
- Require the library where needed: `const O = require('string-utilz');`
- Require the library where needed: `const stringz = require('string-utilz');`
- Manage strings in a much more seamless way.
# Default behavior
By default, you will need to use the object returned from the `require` statement to manage strings. As an example:
```
const stringz = require('string-utilz');
stringz.startsWith('Bob','B'); // true
stringz.endsWith('Bob','o'); // false
```
## #addStringPrototypes()
If you are so inclined to let `string-utilz` try to prototype the `String` object, feel free to call the `stringz.addStringPrototypes()` method. This will *non-destructively* add each of the functions to the `String` object, if there is no existing definition. This can safely be called multiple times, without concern.
# String helpers

@@ -85,5 +96,2 @@ Depending on the version of Node.js (or where your application is running), you might or might not have access to `String#startsWith(str)` and `String#endsWith(str)`. This module adds that functionality, as well as methods for `String#containsIgnoreCase(str)` and `String#replaceAll(oldStr,newStr)`. These are documented below:

## Important Note about Stringz
These are non-destructive functional additions. EACH of these will check to ensure there is not already functionality that does the same thing, and only add the functionality if it doesn't exist. This means this is safe to use in any of your environments.
# Slack

@@ -90,0 +98,0 @@ This is one of several projects that are in the works, so feel free to reach out on [Slack](https://maddhacker.slack.com/). Please email `slack at maddhacker dot com` for an invite.

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc