string-utilz
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -320,2 +320,50 @@ /** | ||
}); | ||
/** | ||
* check stringz#times(tmpStr, num) | ||
*/ | ||
describe('stringz#times(tmpStr, num)', function () { | ||
it('should return the string when multiplied by 1', function () { | ||
expect(stringz.times('*', 1)).toBe('*'); | ||
}); | ||
it('should return double the string when multiplied by 2', function () { | ||
expect(stringz.times('*', 2)).toBe('**'); | ||
}); | ||
it('should return multiples of the string when multiplied', function () { | ||
expect(stringz.times('*', 10)).toBe('**********'); | ||
}); | ||
it('should return multiples of a complex string', function () { | ||
expect(stringz.times('quick brown', 2)).toBe('quick brownquick brown'); | ||
}); | ||
it('should return null when multipled by 0', function () { | ||
expect(stringz.times('*', 0)).toBe(null); | ||
}); | ||
it('should return itself when multipled by a negative number', function () { | ||
expect(stringz.times('*', -2)).toBe('*'); | ||
}); | ||
}); | ||
/** | ||
* check stringz#pad(tmpStr, num, char) | ||
*/ | ||
describe('stringz#pad(tmpStr, num, char)', function () { | ||
it('should use a space when no char is provided', function () { | ||
expect(stringz.pad('*', 1)).toBe('* '); | ||
}); | ||
it('should return the given string when num is 0', function () { | ||
expect(stringz.pad('*', 0)).toBe('*'); | ||
expect(stringz.pad('*', 0, '-')).toBe('*'); | ||
}); | ||
it('should pad multiple times', function () { | ||
expect(stringz.pad('*', 5, '-')).toBe('*-----'); | ||
}); | ||
it('should pad to the left when negative', function () { | ||
expect(stringz.pad('*', -5, '-')).toBe('-----*'); | ||
}); | ||
it('should handle multiple chars', function () { | ||
expect(stringz.pad('*', -2, 'bob')).toBe('bobbob*'); | ||
expect(stringz.pad('*', 2, 'bob')).toBe('*bobbob'); | ||
expect(stringz.pad('*', 2, '$$')).toBe('*$$$$'); | ||
}); | ||
}); | ||
}); |
@@ -0,1 +1,6 @@ | ||
# 2017-04-02 - v1.1.0 | ||
- Update [Jest](https://www.npmjs.com/package/jest) version | ||
- Support `#times` method to multiply a string | ||
- Support `#pad` method to left/right pad a given string | ||
# 2017-02-26 - v1.0.0 | ||
@@ -2,0 +7,0 @@ - Update test names to more closely match lib names |
@@ -18,6 +18,6 @@ /** | ||
/* | ||
* MAKE STRINGS USEFUL => Prototypes for String so it's not as annoying... | ||
* MAKE STRINGS USEFUL => (optional) Prototypes for String so it's not as annoying... | ||
* | ||
* To use, simply include: | ||
* require('stringz'); | ||
* require('string-utilz'); | ||
* in your main entry point (typically index.js) | ||
@@ -168,2 +168,39 @@ */ | ||
/** | ||
* Multiplication for a given string. | ||
* | ||
* @param {string} string to multiply | ||
* @param {number} number of times to multiply string | ||
* @return {string} that is multiplied the given number of times. Multiplying by 0 will return {null}. Multiplying by a negative number or 1 will return the given string only | ||
* | ||
* @usage '*'.times(2); => '**' | ||
*/ | ||
const times = (tmpStr, num) => { | ||
var tmpRtn = tmpStr; | ||
if (num == 0) { | ||
tmpRtn = null; | ||
} else if (num > 1) { | ||
for (var i = 1; i < num; i++) { | ||
tmpRtn += tmpStr; | ||
} | ||
} | ||
return tmpRtn; | ||
} | ||
/** | ||
* Padding for a string, either left (negative) or right (positive) | ||
* | ||
* @param {string} string to pad | ||
* @param {number} number of times to repeat given padding character(s) | ||
* @param {string} string to use as a padding character(s). Defaults to ' ' (empty string) when not provided | ||
* @return {string} with left padding (if given size is negative) or right padding (if given size is positive). Will return the string without any padding when size = 0 | ||
*/ | ||
const pad = (tmpStr, size, char) => { | ||
if (size == 0) | ||
return tmpStr; | ||
char = char || ' '; | ||
var padding = times(char, Math.abs(size)); | ||
return ((size > 0) ? (tmpStr + padding) : (padding + tmpStr)); | ||
} | ||
/** | ||
* Adds the following to the `String.prototype` if it's not already a function: | ||
@@ -177,2 +214,4 @@ * - startsWith | ||
* - fmt | ||
* - times | ||
* - pad | ||
* | ||
@@ -207,2 +246,8 @@ * Also sets `String.fmt` = fmt | ||
String.fmt = fmt; | ||
if (typeof String.prototype.times != 'function') | ||
String.prototype.times = function (num) { return times.apply(null, this, num); } | ||
if (typeof String.prototype.pad != 'function') | ||
String.prototype.pad = function (num, char) { return pad.apply(null, this, num, char); } | ||
} | ||
@@ -218,3 +263,5 @@ | ||
fmt: fmt, | ||
times: times, | ||
pad: pad, | ||
addStringPrototypes: addPrototypes | ||
} |
{ | ||
"name": "string-utilz", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "String utilities for JavaScript.", | ||
@@ -31,3 +31,3 @@ "main": "lib/string-utilz.js", | ||
"devDependencies": { | ||
"jest": "^18.1.0" | ||
"jest": "^19.0.2" | ||
}, | ||
@@ -34,0 +34,0 @@ "jest": { |
# String Utilz | ||
String management for JavaScript. | ||
String management, tools and tricks for JavaScript. | ||
@@ -17,3 +17,3 @@ ## The Fun Stuff | ||
## Why *Another* Output/Logging tool? | ||
## Why *Another* String formatting tool? | ||
Several npms utilize multiple dependencies, and lead to code bloat. There are also several modules on the market that are very opinionated (e.g. force you to do certain things) or are focused on a single form of string management. This tool aims to be a lightweight, flexible solution that allows for infinitely customizable options to suit user needs. | ||
@@ -47,2 +47,4 @@ | ||
- `String.fmt(fmtString, args...)` => same functionality as `string#fmt(args...)`, just an extension of the `String` class, rather than object. | ||
- `string#times(num)` => returns the `String` multipled by `num`, multiplying by 0 => `null` and muliplying by 1 or any negative number returns the `String` | ||
- `string#pad(num,char)` => pads the given `String` `num` times using the given `char`. `char` can be one or more characters, and is optional (default value is an empty space (`' '`)). Negative `num` pads left, postive `num` pads right, and `0` as `num` doesn't pad. | ||
@@ -95,2 +97,15 @@ Usage examples: | ||
// times | ||
'*'.times(2); // '**' | ||
'bob'.times(3); // 'bobbobbob' | ||
'frank'.times(0); // null | ||
'frank'.times(-2); // 'frank' | ||
// pad | ||
'-'.pad(1); // '- ' | ||
'frank'.pad(2, '-'); // 'frank--' | ||
'frank'.pad(-2,'-'); // '--frank' | ||
'bob'.pad(0); // 'bob' | ||
'bob'.pad(1 'frank'); // 'bobfrank' | ||
``` | ||
@@ -97,0 +112,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
46059
608
118
0