diff --git a/API.md b/API.md index 1f33d898..a2414c02 100644 --- a/API.md +++ b/API.md @@ -1070,14 +1070,17 @@ Object.isInstance({foo: 'bar'}); // true * _instance_ * [.camelCase()](#String+camelCase) ⇒ string * [.capitalize([allWords])](#String+capitalize) ⇒ string + * [.chars()](#String+chars) ⇒ Array.<string> * [.contains(pattern)](#String+contains) ⇒ Array.<string> * [.decapitalize([allWords])](#String+decapitalize) ⇒ string + * [.humanize()](#String+humanize) ⇒ string * [.kebabCase()](#String+kebabCase) ⇒ string * [.lines()](#String+lines) ⇒ Array.<string> * [.mask([num], [mask])](#String+mask) ⇒ string * [.pluralize(value, [plural])](#String+pluralize) ⇒ string * [.reverse()](#String+reverse) ⇒ string * [.snakeCase()](#String+snakeCase) ⇒ string + * [.swapCase()](#String+swapCase) ⇒ string * [.truncate(num, [truncateString])](#String+truncate) ⇒ string * [.words(pattern)](#String+words) ⇒ Array.<string> * _static_ @@ -1112,6 +1115,16 @@ Returns the capitalized string 'foo bar'.capitalize(); // 'Foo bar' 'hello world'.capitalize(true); // 'Hello World' ``` + + +### string.chars() ⇒ Array.<string> +Returns an array of the string's character + +**Kind**: instance method of [String](#String) +**Example** +```javascript +'Hello'.chars(); // ['H', 'e', 'l', 'l', 'o'] +``` ### string.contains(pattern) ⇒ Array.<string> @@ -1144,6 +1157,16 @@ Returns the decapitalized string 'Foo Bar'.decapitalize(); // 'foo Bar' 'Hello World'.decapitalize(true); // 'hello world' ``` + + +### string.humanize() ⇒ string +Converts an underscored, camelized, or dasherized string into a humanized one. Also removes beginning and ending whitespace + +**Kind**: instance method of [String](#String) +**Example** +```javascript +' capitalize dash-CamelCase_underscore trim '.humanize(); // 'Capitalize dash camel case underscore trim' +``` ### string.kebabCase() ⇒ string @@ -1229,6 +1252,16 @@ Converts a string to snake case 'AllThe-small Things'.snakeCase(); // "all_the_small_things" 'IAmListeningToFMWhileLoadingDifferentURLOnMyBrowserAndAlsoEditingSomeXMLAndHTML'.snakeCase(); // 'i_am_listening_to_fm_while_loading_different_url_on_my_browser_and_also_editing_some_xml_and_html' ``` + + +### string.swapCase() ⇒ string +Returns a copy of the string in which all the case-based characters have had their case swapped + +**Kind**: instance method of [String](#String) +**Example** +```javascript +'Hello'.swapCase(); // 'hELLO' +``` ### string.truncate(num, [truncateString]) ⇒ string @@ -1239,7 +1272,7 @@ Truncates a string up to a specified length | Param | Type | Default | | --- | --- | --- | | num | number | | -| [truncateString] | string | "\"...\"" | +| [truncateString] | string | "..." | **Example** ```javascript diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b547d4b..e256b747 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,13 @@ # Changelog -## [next](https://github.com/ardalanamini/prototyped.js/releases/tag/next) *(2018-__-__)* +## [v0.4.0](https://github.com/ardalanamini/prototyped.js/releases/tag/v0.4.0) *(2018-01-28)* **Implemented enhancements:** - more customizable usage - `String.prototype` - function `truncate` parameter `truncateString` added + - function `chars` added + - function `swapCase` added + - function `humanize` added ## [v0.3.2](https://github.com/ardalanamini/prototyped.js/releases/tag/v0.3.2) *(2018-01-25)* diff --git a/README.md b/README.md index d0286249..d1199040 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

Prototyped.JS

-

Common Typescript ready prototypes available in both ES6 and ES5

+

Common Typescript ready prototypes available in ES5 and ES6, Server-Side and Client-Side

npm version @@ -16,9 +16,12 @@ tested with jest +
-[TOC] +- - - + +> if you have a method you think needs to be a part of this package, feel free to contribute ## Installation diff --git a/lib/string/chars/index.ts b/lib/string/chars/index.ts new file mode 100644 index 00000000..e63edabc --- /dev/null +++ b/lib/string/chars/index.ts @@ -0,0 +1,18 @@ +export { } + +declare global { + interface String { + chars(): Array + } +} + +/** + * Returns an array of the string's character + * @memberof String + * @returns {string[]} + * @example + * 'Hello'.chars(); // ['H', 'e', 'l', 'l', 'o'] + */ +String.prototype.chars = function(): Array { + return this.split('') +} diff --git a/lib/string/chars/test.js b/lib/string/chars/test.js new file mode 100644 index 00000000..987dfe8a --- /dev/null +++ b/lib/string/chars/test.js @@ -0,0 +1,8 @@ +require('../../../es6/string/chars') + +describe("String.prototype.chars", () => { + test("'Hello'.chars() returns ['H', 'e', 'l', 'l', 'o']", () => { + expect('Hello'.chars()) + .toEqual(['H', 'e', 'l', 'l', 'o']) + }) +}) diff --git a/lib/string/humanize/index.ts b/lib/string/humanize/index.ts new file mode 100644 index 00000000..2b491496 --- /dev/null +++ b/lib/string/humanize/index.ts @@ -0,0 +1,20 @@ +export { } + +declare global { + interface String { + humanize(): string + } +} + +/** + * Converts an underscored, camelized, or dasherized string into a humanized one. Also removes beginning and ending whitespace + * @memberof String + * @returns {string} + * @example + * ' capitalize dash-CamelCase_underscore trim '.humanize(); // 'Capitalize dash camel case underscore trim' + */ +String.prototype.humanize = function(): string { + let s = this.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g) || [''] + + return s.map(x => x.toLowerCase()).join(' ').replace(/^[a-z]/, (char) => char.toUpperCase()) +} diff --git a/lib/string/humanize/test.js b/lib/string/humanize/test.js new file mode 100644 index 00000000..617b0a44 --- /dev/null +++ b/lib/string/humanize/test.js @@ -0,0 +1,8 @@ +require('../../../es6/string/humanize') + +describe("String.prototype.humanize", () => { + test("' capitalize dash-CamelCase_underscore trim '.humanize() returns 'Capitalize dash camel case underscore trim'", () => { + expect(' capitalize dash-CamelCase_underscore trim '.humanize()) + .toBe('Capitalize dash camel case underscore trim') + }) +}) diff --git a/lib/string/index.ts b/lib/string/index.ts index 13fd42ef..d7466245 100644 --- a/lib/string/index.ts +++ b/lib/string/index.ts @@ -2,8 +2,10 @@ import './camelCase' import './capitalize' +import './chars' import './contains' import './decapitalize' +import './humanize' import './isInstance' import './kebabCase' import './lines' @@ -11,5 +13,6 @@ import './mask' import './pluralize' import './reverse' import './snakeCase' +import './swapCase' import './truncate' import './words' diff --git a/lib/string/swapCase/index.ts b/lib/string/swapCase/index.ts new file mode 100644 index 00000000..2f903ac4 --- /dev/null +++ b/lib/string/swapCase/index.ts @@ -0,0 +1,18 @@ +export { } + +declare global { + interface String { + swapCase(): string + } +} + +/** + * Returns a copy of the string in which all the case-based characters have had their case swapped + * @memberof String + * @returns {string} + * @example + * 'Hello'.swapCase(); // 'hELLO' + */ +String.prototype.swapCase = function(): string { + return this.replace(/\S/g, (c) => c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase()) +} diff --git a/lib/string/swapCase/test.js b/lib/string/swapCase/test.js new file mode 100644 index 00000000..6e917c71 --- /dev/null +++ b/lib/string/swapCase/test.js @@ -0,0 +1,8 @@ +require('../../../es6/string/swapCase') + +describe("String.prototype.swapCase", () => { + test("'Hello'.swapCase() returns 'hELLO'", () => { + expect('Hello'.swapCase()) + .toBe('hELLO') + }) +}) diff --git a/lib/string/truncate/index.ts b/lib/string/truncate/index.ts index 2e620bcb..d1b5d3cb 100644 --- a/lib/string/truncate/index.ts +++ b/lib/string/truncate/index.ts @@ -10,7 +10,7 @@ declare global { * Truncates a string up to a specified length * @memberof String * @param {number} num - * @param {string} [truncateString="..."] + * @param {string} [truncateString=...] * @returns {string} * @example * 'boomerang'.truncate(7); // 'boom...' diff --git a/package.json b/package.json index 252b7b79..a162ff24 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "prototyped.js", - "version": "0.3.2", - "description": "Common typescript ready prototypes available in both es6 and es5", + "version": "0.4.0", + "description": "Common typescript ready prototypes available in both es5 and es6", "author": "Ardalan Amini ", "license": "MIT", "homepage": "https://github.com/ardalanamini/prototyped.js#readme", @@ -66,6 +66,9 @@ "defer", "cache", "string", + "chars", + "humanize", + "swapCase", "capitalize", "decapitalize", "mask",