Skip to content

Commit

Permalink
4 new methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardalan Amini committed Jan 25, 2018
1 parent dc7c657 commit e09fce2
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 3 deletions.
56 changes: 56 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* [.avg](#Array+avg) ⇒ <code>number</code>
* [.first()](#Array+first) ⇒ <code>\*</code>
* [.last()](#Array+last) ⇒ <code>\*</code>
* [.initial()](#Array+initial)[<code>Array</code>](#Array)
* [.tail()](#Array+tail)[<code>Array</code>](#Array)
* [.chunk(size)](#Array+chunk)[<code>Array.&lt;Array&gt;</code>](#Array)
* [.compact()](#Array+compact)[<code>Array</code>](#Array)
* [.count([value])](#Array+count) ⇒ <code>number</code>
Expand Down Expand Up @@ -60,6 +62,7 @@
* [.clone()](#Array+clone)[<code>Array</code>](#Array)
* [.median()](#Array+median)[<code>Array</code>](#Array)
* [.pad(size, [value])](#Array+pad)[<code>Array</code>](#Array)
* [.append(value)](#Array+append)
* [.prepend(value)](#Array+prepend)
* _static_
* [.isInstance](#Array.isInstance) ⇒ <code>boolean</code>
Expand Down Expand Up @@ -103,6 +106,26 @@ Returns the last item of the array
```javascript
[1, 2, 3].last(); // 3
```
<a name="Array+initial"></a>

### array.initial() ⇒ [<code>Array</code>](#Array)
Returns all the elements of an array except the last one

**Kind**: instance method of [<code>Array</code>](#Array)
**Example**
```javascript
[1, 2, 3].initial(); // [1, 2]
```
<a name="Array+tail"></a>

### array.tail() ⇒ [<code>Array</code>](#Array)
Returns all elements in an array except for the first one

**Kind**: instance method of [<code>Array</code>](#Array)
**Example**
```javascript
[1, 2, 3].tail(); // [2, 3]
```
<a name="Array+chunk"></a>

### array.chunk(size) ⇒ [<code>Array.&lt;Array&gt;</code>](#Array)
Expand Down Expand Up @@ -556,6 +579,22 @@ FillS the array with the given value until the array reaches the specified size
[1, 2, 3].pad(5, 0); // [1, 2, 3, 0, 0]
[1, 2, 3].pad(-5, 0); // [0, 0, 1, 2, 3]
```
<a name="Array+append"></a>

### array.append(value)
Same as push but uses concat

**Kind**: instance method of [<code>Array</code>](#Array)

| Param | Type |
| --- | --- |
| value | <code>\*</code> |

**Example**
```javascript
var myArray = [1, 2, 3]
myArray.append(0); // myArray => [1, 2, 3, 0]
```
<a name="Array+prepend"></a>

### array.prepend(value)
Expand Down Expand Up @@ -1040,6 +1079,7 @@ Object.isInstance({foo: 'bar'}); // true
* [.snakeCase()](#String+snakeCase) ⇒ <code>string</code>
* [.truncate(num)](#String+truncate) ⇒ <code>string</code>
* [.words(pattern)](#String+words) ⇒ <code>Array.&lt;string&gt;</code>
* [.contains(pattern)](#String+contains) ⇒ <code>Array.&lt;string&gt;</code>
* _static_
* [.isInstance(arg)](#String.isInstance) ⇒ <code>boolean</code>

Expand Down Expand Up @@ -1204,6 +1244,22 @@ Converts a given string into an array of words
'I love javaScript!!'.words(); // ["I", "love", "javaScript"]
'python, javaScript & coffee'.words(); // ["python", "javaScript", "coffee"]
```
<a name="String+contains"></a>

### string.contains(pattern) ⇒ <code>Array.&lt;string&gt;</code>
Find out if the string contains the argument at any position

**Kind**: instance method of [<code>String</code>](#String)

| Param | Type |
| --- | --- |
| pattern | <code>RegExp</code> |

**Example**
```javascript
'javaScript & typescript'.contains('Typescript'); // true
'javaScript & typescript'.contains('Typescript', true); // false
```
<a name="String.isInstance"></a>

### String.isInstance(arg) ⇒ <code>boolean</code>
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## [v0.3.2](https://github.com/ardalanamini/prototyped.js/releases/tag/v0.3.2) *(2018-01-25)*
**Implemented enhancements:**
- `Array.prototype`
- function `initial` added
- function `tail` added
- function `append` added
- `String.prototype`
- function `contains` added


## [v0.3.1](https://github.com/ardalanamini/prototyped.js/releases/tag/v0.3.1) *(2018-01-24)*
**Implemented enhancements:**
- es6 usage is now available along side es5 (dist)
Expand Down
16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prototyped.js",
"version": "0.3.1",
"version": "0.3.2",
"description": "Some common typescript ready prototypes available in both es6 and es5",
"author": "Ardalan Amini <[email protected]>",
"license": "MIT",
Expand All @@ -24,6 +24,8 @@
"avg",
"first",
"last",
"initial",
"tail",
"chunk",
"compact",
"count",
Expand Down Expand Up @@ -55,6 +57,7 @@
"clone",
"median",
"pad",
"append",
"prepend",
"boolean",
"date",
Expand All @@ -63,6 +66,17 @@
"defer",
"cache",
"string",
"capitalize",
"decapitalize",
"mask",
"pluralize",
"reverse",
"lines",
"camelCase",
"kebabCase",
"snakeCase",
"truncate",
"words",
"math",
"factorial",
"fibonacci",
Expand Down
44 changes: 44 additions & 0 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ declare global {
interface Array<T> {
first(): T
last(): T
initial(): Array<T>
tail(): Array<T>
chunk(size: number): Array<Array<T>>
compact(): Array<T>
count(value?: T): number
Expand Down Expand Up @@ -46,6 +48,7 @@ declare global {
clone(): Array<T>
median(key?: string): number
pad(size: number, value?: any): Array<any>
append(value?: any): void
prepend(value?: any): void
}
}
Expand Down Expand Up @@ -113,6 +116,30 @@ if (!Array.prototype.last) {
}
}

if (!Array.prototype.initial) {
/**
* Returns all the elements of an array except the last one
* @returns {Array}
* @example
* [1, 2, 3].initial(); // [1, 2]
*/
Array.prototype.initial = function(): Array<any> {
return this.slice(0, -1)
}
}

if (!Array.prototype.tail) {
/**
* Returns all elements in an array except for the first one
* @returns {Array}
* @example
* [1, 2, 3].tail(); // [2, 3]
*/
Array.prototype.tail = function(): Array<any> {
return this.length > 1 ? this.slice(1) : []
}
}

if (!Array.prototype.chunk) {
/**
* Chunks the array into smaller arrays of a specified size
Expand Down Expand Up @@ -715,6 +742,23 @@ if (!Array.prototype.pad) {
}
}

if (!Array.prototype.append) {
/**
* Same as push but uses concat
* @param {*} value
* @example
* var myArray = [1, 2, 3]
* myArray.append(0); // myArray => [1, 2, 3, 0]
*/
Array.prototype.append = function(value: any): void {
let array = this.concat([value])

this.length = 0

this.push(...array)
}
}

if (!Array.prototype.prepend) {
/**
* Adds an item to the beginning of the array
Expand Down
17 changes: 17 additions & 0 deletions src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ declare global {
snakeCase(): string
truncate(num: number): String
words(pattern?: RegExp): Array<string>
contains(str: string): boolean
}
}

Expand Down Expand Up @@ -214,3 +215,19 @@ if (!String.prototype.words) {
return this.split(pattern).filter(Boolean)
}
}

if (!String.prototype.contains) {
/**
* Find out if the string contains the argument at any position
* @param {RegExp} pattern
* @returns {string[]}
* @example
* 'javaScript & typescript'.contains('Typescript'); // true
* 'javaScript & typescript'.contains('Typescript', true); // false
*/
String.prototype.contains = function(str: string, sensitive = false): boolean {
if (sensitive) return this.indexOf(str) !== -1

return this.toLowerCase().indexOf(str.toLowerCase()) !== -1
}
}
24 changes: 23 additions & 1 deletion tests/array.es6.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ describe("Array.prototype.last", () => {
})
})

describe("Array.prototype.initial", () => {
test("[1, 2, 3].initial() returns [1, 2]", () => {
expect([1,2,3].initial()).toEqual([1, 2])
})
})

describe("Array.prototype.tail", () => {
test("[1, 2, 3].tail() returns [2, 3]", () => {
expect([1,2,3].tail()).toEqual([2, 3])
})
})

describe("Array.prototype.chunk", () => {
test("[1, 2, 3, 4, 5].chunk(2) returns [[1,2],[3,4],[5]]", () => {
expect([1,2,3,4,5].chunk(2)).toEqual([[1,2],[3,4],[5]])
Expand Down Expand Up @@ -314,8 +326,18 @@ describe("Array.prototype.pad", () => {
})
})

describe("Array.prototype.append", () => {
test("myArray = [1, 2, 3] & myArray.append(0) results myArray to be [1, 2, 3, 0]", () => {
expect((() => {
let myArray = [1, 2, 3]
myArray.append(0)
return myArray
})()).toEqual([1, 2, 3, 0])
})
})

describe("Array.prototype.prepend", () => {
test("[1, 2, 3].prepend(0) returns [0, 1, 2, 3]", () => {
test("myArray = [1, 2, 3] & myArray.prepend(0) results myArray to be [0, 1, 2, 3]", () => {
expect((() => {
let myArray = [1, 2, 3]
myArray.prepend(0)
Expand Down
24 changes: 23 additions & 1 deletion tests/array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ describe("Array.prototype.last", () => {
})
})

describe("Array.prototype.initial", () => {
test("[1, 2, 3].initial() returns [1, 2]", () => {
expect([1,2,3].initial()).toEqual([1, 2])
})
})

describe("Array.prototype.tail", () => {
test("[1, 2, 3].tail() returns [2, 3]", () => {
expect([1,2,3].tail()).toEqual([2, 3])
})
})

describe("Array.prototype.chunk", () => {
test("[1, 2, 3, 4, 5].chunk(2) returns [[1,2],[3,4],[5]]", () => {
expect([1,2,3,4,5].chunk(2)).toEqual([[1,2],[3,4],[5]])
Expand Down Expand Up @@ -314,8 +326,18 @@ describe("Array.prototype.pad", () => {
})
})

describe("Array.prototype.append", () => {
test("myArray = [1, 2, 3] & myArray.append(0) results myArray to be [1, 2, 3, 0]", () => {
expect((() => {
let myArray = [1, 2, 3]
myArray.append(0)
return myArray
})()).toEqual([1, 2, 3, 0])
})
})

describe("Array.prototype.prepend", () => {
test("[1, 2, 3].prepend(0) returns [0, 1, 2, 3]", () => {
test("myArray = [1, 2, 3] & myArray.prepend(0) results myArray to be [0, 1, 2, 3]", () => {
expect((() => {
let myArray = [1, 2, 3]
myArray.prepend(0)
Expand Down
12 changes: 12 additions & 0 deletions tests/string.es6.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,15 @@ describe("String.prototype.words", () => {
.toEqual(['python', 'javaScript','coffee'])
})
})

describe("String.prototype.contains", () => {
test("'javaScript & typescript'.contains('Typescript'); // true", () => {
expect('javaScript & typescript'.contains('Typescript'))
.toBe(true)
})

test("'javaScript & typescript'.contains('Typescript', true); // false", () => {
expect('javaScript & typescript'.contains('Typescript', true))
.toBe(false)
})
})
12 changes: 12 additions & 0 deletions tests/string.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,15 @@ describe("String.prototype.words", () => {
.toEqual(['python', 'javaScript','coffee'])
})
})

describe("String.prototype.contains", () => {
test("'javaScript & typescript'.contains('Typescript'); // true", () => {
expect('javaScript & typescript'.contains('Typescript'))
.toBe(true)
})

test("'javaScript & typescript'.contains('Typescript', true); // false", () => {
expect('javaScript & typescript'.contains('Typescript', true))
.toBe(false)
})
})

0 comments on commit e09fce2

Please sign in to comment.