Skip to content

Commit

Permalink
3 new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardalan Amini committed Jan 28, 2018
1 parent e9240ba commit 10751a2
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 7 deletions.
35 changes: 34 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -1070,14 +1070,17 @@ Object.isInstance({foo: 'bar'}); // true
* _instance_
* [.camelCase()](#String+camelCase) ⇒ <code>string</code>
* [.capitalize([allWords])](#String+capitalize) ⇒ <code>string</code>
* [.chars()](#String+chars) ⇒ <code>Array.&lt;string&gt;</code>
* [.contains(pattern)](#String+contains) ⇒ <code>Array.&lt;string&gt;</code>
* [.decapitalize([allWords])](#String+decapitalize) ⇒ <code>string</code>
* [.humanize()](#String+humanize) ⇒ <code>string</code>
* [.kebabCase()](#String+kebabCase) ⇒ <code>string</code>
* [.lines()](#String+lines) ⇒ <code>Array.&lt;string&gt;</code>
* [.mask([num], [mask])](#String+mask) ⇒ <code>string</code>
* [.pluralize(value, [plural])](#String+pluralize) ⇒ <code>string</code>
* [.reverse()](#String+reverse) ⇒ <code>string</code>
* [.snakeCase()](#String+snakeCase) ⇒ <code>string</code>
* [.swapCase()](#String+swapCase) ⇒ <code>string</code>
* [.truncate(num, [truncateString])](#String+truncate) ⇒ <code>string</code>
* [.words(pattern)](#String+words) ⇒ <code>Array.&lt;string&gt;</code>
* _static_
Expand Down Expand Up @@ -1112,6 +1115,16 @@ Returns the capitalized string
'foo bar'.capitalize(); // 'Foo bar'
'hello world'.capitalize(true); // 'Hello World'
```
<a name="String+chars"></a>

### string.chars() ⇒ <code>Array.&lt;string&gt;</code>
Returns an array of the string's character

**Kind**: instance method of [<code>String</code>](#String)
**Example**
```javascript
'Hello'.chars(); // ['H', 'e', 'l', 'l', 'o']
```
<a name="String+contains"></a>

### string.contains(pattern) ⇒ <code>Array.&lt;string&gt;</code>
Expand Down Expand Up @@ -1144,6 +1157,16 @@ Returns the decapitalized string
'Foo Bar'.decapitalize(); // 'foo Bar'
'Hello World'.decapitalize(true); // 'hello world'
```
<a name="String+humanize"></a>

### string.humanize() ⇒ <code>string</code>
Converts an underscored, camelized, or dasherized string into a humanized one. Also removes beginning and ending whitespace

**Kind**: instance method of [<code>String</code>](#String)
**Example**
```javascript
' capitalize dash-CamelCase_underscore trim '.humanize(); // 'Capitalize dash camel case underscore trim'
```
<a name="String+kebabCase"></a>

### string.kebabCase() ⇒ <code>string</code>
Expand Down Expand Up @@ -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'
```
<a name="String+swapCase"></a>

### string.swapCase() ⇒ <code>string</code>
Returns a copy of the string in which all the case-based characters have had their case swapped

**Kind**: instance method of [<code>String</code>](#String)
**Example**
```javascript
'Hello'.swapCase(); // 'hELLO'
```
<a name="String+truncate"></a>

### string.truncate(num, [truncateString]) ⇒ <code>string</code>
Expand All @@ -1239,7 +1272,7 @@ Truncates a string up to a specified length
| Param | Type | Default |
| --- | --- | --- |
| num | <code>number</code> | |
| [truncateString] | <code>string</code> | <code>&quot;\&quot;...\&quot;&quot;</code> |
| [truncateString] | <code>string</code> | <code>&quot;...&quot;</code> |

**Example**
```javascript
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)*
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div align="center">
<h1>Prototyped.JS</h1>
<p>Common <b>Typescript ready</b> prototypes available in both <b>ES6</b> and <b>ES5</b></p>
<p>Common <b>Typescript ready</b> prototypes available in <b>ES5</b> and <b>ES6</b>, <b>Server-Side</b> and <b>Client-Side</b></p>
<a href="https://www.npmjs.com/package/prototyped.js" target="_blank">
<img src="https://img.shields.io/npm/v/prototyped.js.svg" alt="npm version">
</a>
Expand All @@ -16,9 +16,12 @@
<a href="https://github.com/ardalanamini/prototyped.js/blob/master/LICENSE" target="_blank">
<img src="https://img.shields.io/github/license/ardalanamini/prototyped.js.svg" alt="tested with jest">
</a>
<br>
</div>

[TOC]
- - -

> if you have a method you think needs to be a part of this package, feel free to contribute
## Installation

Expand Down
18 changes: 18 additions & 0 deletions lib/string/chars/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export { }

declare global {
interface String {
chars(): Array<string>
}
}

/**
* 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<string> {
return this.split('')
}
8 changes: 8 additions & 0 deletions lib/string/chars/test.js
Original file line number Diff line number Diff line change
@@ -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'])
})
})
20 changes: 20 additions & 0 deletions lib/string/humanize/index.ts
Original file line number Diff line number Diff line change
@@ -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())
}
8 changes: 8 additions & 0 deletions lib/string/humanize/test.js
Original file line number Diff line number Diff line change
@@ -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')
})
})
3 changes: 3 additions & 0 deletions lib/string/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import './camelCase'
import './capitalize'
import './chars'
import './contains'
import './decapitalize'
import './humanize'
import './isInstance'
import './kebabCase'
import './lines'
import './mask'
import './pluralize'
import './reverse'
import './snakeCase'
import './swapCase'
import './truncate'
import './words'
18 changes: 18 additions & 0 deletions lib/string/swapCase/index.ts
Original file line number Diff line number Diff line change
@@ -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())
}
8 changes: 8 additions & 0 deletions lib/string/swapCase/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require('../../../es6/string/swapCase')

describe("String.prototype.swapCase", () => {
test("'Hello'.swapCase() returns 'hELLO'", () => {
expect('Hello'.swapCase())
.toBe('hELLO')
})
})
2 changes: 1 addition & 1 deletion lib/string/truncate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...'
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"license": "MIT",
"homepage": "https://github.com/ardalanamini/prototyped.js#readme",
Expand Down Expand Up @@ -66,6 +66,9 @@
"defer",
"cache",
"string",
"chars",
"humanize",
"swapCase",
"capitalize",
"decapitalize",
"mask",
Expand Down

0 comments on commit 10751a2

Please sign in to comment.