Skip to content

Commit

Permalink
so many changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardalan Amini committed Jan 28, 2018
1 parent e09fce2 commit e9240ba
Show file tree
Hide file tree
Showing 182 changed files with 3,003 additions and 3,159 deletions.
660 changes: 331 additions & 329 deletions API.md

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [next](https://github.com/ardalanamini/prototyped.js/releases/tag/next) *(2018-__-__)*
**Implemented enhancements:**
- more customizable usage
- `String.prototype`
- function `truncate` parameter `truncateString` added


## [v0.3.2](https://github.com/ardalanamini/prototyped.js/releases/tag/v0.3.2) *(2018-01-25)*
**Implemented enhancements:**
- `Array.prototype`
Expand Down
44 changes: 35 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
# prototyped.js

Some common **Typescript ready** prototypes available in both es6 and es5

[![npm](https://img.shields.io/npm/v/prototyped.js.svg)](https://www.npmjs.com/package/prototyped.js)
[![npm](https://img.shields.io/npm/dm/prototyped.js.svg)](https://www.npmjs.com/package/prototyped.js)
[![GitHub stars](https://img.shields.io/github/stars/ardalanamini/prototyped.js.svg)](https://github.com/ardalanamini/prototyped.js/stargazers)
[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest)
[![license](https://img.shields.io/github/license/ardalanamini/prototyped.js.svg)](https://github.com/ardalanamini/prototyped.js/blob/master/LICENSE)
<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>
<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>
<a href="https://www.npmjs.com/package/prototyped.js" target="_blank">
<img src="https://img.shields.io/npm/dm/prototyped.js.svg" alt="npm monthly downloads">
</a>
<a href="https://github.com/ardalanamini/prototyped.js/stargazers" target="_blank">
<img src="https://img.shields.io/github/stars/ardalanamini/prototyped.js.svg" alt="github stars">
</a>
<a href="https://github.com/facebook/jest" target="_blank">
<img src="https://img.shields.io/badge/tested_with-jest-99424f.svg" alt="tested with jest">
</a>
<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>
</div>

[TOC]

## Installation

```bash
npm i -s prototyped.js
```

## Usage

import all prototypes

```javascript
// es5 (default)
require('prototyped.js');
Expand All @@ -24,6 +38,18 @@ import 'prototyped.js';
// es6
require('prototyped.js/es6');
import 'prototyped.js/es6';

console.log('hello world!'.words()); // ['hello', 'world']
```

or simply import the prototypes you want

```javascript
require('prototyped.js/dist/string');
// or
require('prototyped.js/dist/string/words');

console.log('hello world!'.words()); // ['hello', 'world']
```

All documents are available at [API.md](https://github.com/ardalanamini/prototyped.js/blob/master/API.md)
23 changes: 23 additions & 0 deletions lib/array/append/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export { }

declare global {
interface Array<T> {
append(value?: any): void
}
}

/**
* Same as push but uses concat
* @memberof Array
* @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)
}
11 changes: 11 additions & 0 deletions lib/array/append/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require('../../../es6/array/append')

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])
})
})
50 changes: 50 additions & 0 deletions lib/array/average/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export { }

declare global {
interface Array<T> {
average(key?: string): number
avg(key?: string): number
}
}

/**
* Returns the average value of a given key
* @memberof Array
* @param {String} [key]
* @returns {number}
* @example
* [1, 2, 3].average(); // 2
* [{a: 1}, {a: 2}, {a: 3}].average('a'); // 2
* [{a: {b: 1}}, {a: {b: 2}}, {a: {b: 3}}].average('a.b'); // 2
*/
Array.prototype.average = function(key?: string): number {
let sum = 0

if (key) {
let keys = key.split('.')

this.map((item) => {
keys.map((k) => item = item && item[k] || 0)

sum += item
})

return sum / this.length
}

this.map((number) => sum += number)

return sum / this.length
}

/**
* An alias of Array.prototype.average
* @memberof Array
* @param {String} [key]
* @returns {number}
* @example
* [1, 2, 3].avg(); // 2
* [{a: 1}, {a: 2}, {a: 3}].avg('a'); // 2
* [{a: {b: 1}}, {a: {b: 2}}, {a: {b: 3}}].avg('a.b'); // 2
*/
Array.prototype.avg = Array.prototype.average
15 changes: 15 additions & 0 deletions lib/array/average/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require('../../../es6/array/average')

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

test("[{a: 1}, {a: 2}, {a: 3}].average('a') returns 2", () => {
expect([{a: 1}, {a: 2}, {a: 3}].average('a')).toBe(2)
})

test("[{a: {b: 1}}, {a: {b: 2}}, {a: {b: 3}}].average('a.b') returns 2", () => {
expect([{a: {b: 1}}, {a: {b: 2}}, {a: {b: 3}}].average('a.b')).toBe(2)
})
})
19 changes: 19 additions & 0 deletions lib/array/chunk/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export { }

declare global {
interface Array<T> {
chunk(size: number): Array<Array<T>>
}
}

/**
* Chunks the array into smaller arrays of a specified size
* @memberof Array
* @param {number} size
* @returns {Array[]}
* @example
* [1, 2, 3, 4, 5].chunk(2); // [[1,2],[3,4],[5]]
*/
Array.prototype.chunk = function(size: number): Array<Array<any>> {
return Array.from({ length: Math.ceil(this.length / size) }, (value: any, index: number) => this.slice(index * size, index * size + size))
}
7 changes: 7 additions & 0 deletions lib/array/chunk/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('../../../es6/array/chunk')

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]])
})
})
18 changes: 18 additions & 0 deletions lib/array/clone/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export { }

declare global {
interface Array<T> {
clone(): Array<T>
}
}

/**
* Returns the cloned array
* @memberof Array
* @returns {Array}
* @example
* [1, 2, 3].clone(); // [1, 2, 3]
*/
Array.prototype.clone = function(): Array<any> {
return [...this]
}
7 changes: 7 additions & 0 deletions lib/array/clone/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('../../../es6/array/clone')

describe("Array.prototype.clone", () => {
test("[1, 2, 3].clone() returns [1, 2, 3]", () => {
expect([1, 2, 3].clone()).toEqual([1, 2, 3])
})
})
18 changes: 18 additions & 0 deletions lib/array/compact/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export { }

declare global {
interface Array<T> {
compact(): Array<T>
}
}

/**
* Removes falsey values from the array
* @memberof Array
* @returns {Array}
* @example
* [0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34].compact(); // [ 1, 2, 3, 'a', 's', 34 ]
*/
Array.prototype.compact = function(): Array<any> {
return this.filter(Boolean)
}
7 changes: 7 additions & 0 deletions lib/array/compact/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('../../../es6/array/compact')

describe("Array.prototype.compact", () => {
test("[0,1,false,2,'',3,'a','e' * 23,NaN,'s',34].compact() returns [1,2,3,'a','s',34]", () => {
expect([0,1,false,2,'',3,'a','e' * 23,NaN,'s',34].compact()).toEqual([1,2,3,'a','s',34])
})
})
19 changes: 19 additions & 0 deletions lib/array/contains/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export { }

declare global {
interface Array<T> {
contains(value: any): boolean
}
}

/**
* Determines whether the collection contains a given item
* @memberof Array
* @param {*} value
* @returns {boolean}
* @example
* [1, 2, 3].contains(2); // true
*/
Array.prototype.contains = function(value: any): boolean {
return this.indexOf(value) !== -1
}
7 changes: 7 additions & 0 deletions lib/array/contains/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('../../../es6/array/contains')

describe("Array.prototype.contains", () => {
test("[1, 2, 3].contains(2) returns true", () => {
expect([1, 2, 3].contains(2)).toBe(true)
})
})
20 changes: 20 additions & 0 deletions lib/array/count/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export { }

declare global {
interface Array<T> {
count(value?: any): number
}
}

/**
* Counts the occurrences of a value in an array
* @memberof Array
* @param {*} [value]
* @returns {number}
* @example
* [1, 1, 2, 1, 2, 3].count(); // 6
* [1, 1, 2, 1, 2, 3].count(1); // 3
*/
Array.prototype.count = function(value: any): number {
return value ? this.reduce((a, v) => (v === value ? a + 1 : a + 0), 0) : this.length
}
11 changes: 11 additions & 0 deletions lib/array/count/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require('../../../es6/array/count')

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

test("[1, 1, 2, 1, 2, 3].count(1) returns 3", () => {
expect([1, 1, 2, 1, 2, 3].count(1)).toBe(3)
})
})
24 changes: 24 additions & 0 deletions lib/array/countBy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export { }

declare global {
interface Array<T> {
countBy(fn: string | (() => any)): { [key: string]: any }
}
}

/**
* Groups the elements of an array based on the given function and returns the count of elements in each group
* @memberof Array
* @param {String|Function} fn
* @returns {Object}
* @example
* [6.1, 4.2, 6.3].countBy(Math.floor); // {4: 1, 6: 2}
* ['one', 'two', 'three'].countBy('length'); // {3: 2, 5: 1}
*/
Array.prototype.countBy = function(fn: string | (() => any)): { [key: string]: any } {
return this.map(typeof fn === 'function' ? fn : (val) => val[fn]).reduce((acc: { [key: string]: any }, val: string, i) => {
acc[val] = (acc[val] || 0) + 1

return acc
}, {})
}
11 changes: 11 additions & 0 deletions lib/array/countBy/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require('../../../es6/array/countBy')

describe("Array.prototype.countBy", () => {
test('[6.1, 4.2, 6.3].countBy(Math.floor) returns {4: 1, 6: 2}', () => {
expect([6.1, 4.2, 6.3].countBy(Math.floor)).toEqual({4: 1, 6: 2})
})

test("['one', 'two', 'three'].countBy('length') returns {3: 2, 5: 1}", () => {
expect(['one', 'two', 'three'].countBy('length')).toEqual({3: 2, 5: 1})
})
})
30 changes: 30 additions & 0 deletions lib/array/crossJoin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export { }

declare global {
interface Array<T> {
crossJoin(array: Array<any>): Array<[T, any]>
}
}

/**
* Cross joins the array's values among the given arrays, returning a Cartesian product with all possible permutations
* @memberof Array
* @param {Array} array
* @returns {Array[]}
* @example
* [1, 2].crossJoin(['a', 'b']); // [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
*/
Array.prototype.crossJoin = function(array: Array<any>): Array<[any, any]> {
let joined: Array<[any, any]> = []

this.map((item) => {
array.map((value) => {
joined.push([
item,
value
])
})
})

return joined
}
7 changes: 7 additions & 0 deletions lib/array/crossJoin/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require('../../../es6/array/crossJoin')

describe("Array.prototype.crossJoin", () => {
test("[1, 2].crossJoin(['a', 'b']); returns [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]", () => {
expect([1, 2].crossJoin(['a', 'b'])).toEqual([[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']])
})
})
Loading

0 comments on commit e9240ba

Please sign in to comment.