Skip to content

Commit

Permalink
Merge 94ee199 into 7c54846
Browse files Browse the repository at this point in the history
  • Loading branch information
Bin-Huang committed Dec 4, 2019
2 parents 7c54846 + 94ee199 commit 79d51d2
Show file tree
Hide file tree
Showing 51 changed files with 328 additions and 345 deletions.
62 changes: 23 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Prray -- "Promisified" Array, compatible with normal array, but comes with async
> Prray aims to replace normal Array in some cases for convenience 😄😜
```javascript
import { prray } from 'prray'
const prr = prray(['www.google.com', 'npmjs.org'])
import Prray from 'prray'
const prr = Prray.from(['www.google.com', 'npmjs.org'])

const responses = await prr.mapAsync(fetch)
```
Expand Down Expand Up @@ -79,10 +79,10 @@ yarn add prray
Prray is compatible with normal array. That means you can safely replace normal Array with Prray. And there are [a lots of unit tests](https://github.com/Bin-Huang/prray/tree/master/test) for prray to test compatibility with normal array.

```javascript
import { prray, Prray } from 'prray'
import Prray from 'prray'

const arr = [1, 2, 3]
const prr = prray(arr)
const prr = Prray.from(arr)

prr[0] // 1
prr[prr.length - 1] // 3
Expand Down Expand Up @@ -133,37 +133,21 @@ arr instanceof Prray // false

## Methods

### Package methods

- [prray(array)](#prrayarray)
- [new Prray()](#new-prray)

#### prray(array)

The prray() method creates and returns a new Prray instance with every element in the array.

```javascript
import { prray } from 'prray'

const prr = prray([1, 2, 3])
console.log(prr[0]) // 1
```

#### new Prray()
### Class Prray

The class `Prray`. You can think of it as class `Array`.

```javascript
import { Prray } from 'prray'
import Prray from 'prray'

const p1 = new Prray()
const p2 = new Prray(1)
const p3 = new Prray('a', 'b')
const p2 = new Prray('a', 'b')
const p3 = Prray.from([1, 2, 3, 4])

console.log(p3[0]) // 'a'
console.log(p2[0]) // 'a'
```

> **Instead `new Prray()`, use methods `prray`, `Prray.from` or `Prray.of` if you want to create a new prray instance**. Because the class Prray is so compatible with class Array, some "weird" behaviors that exists in `new Array()` can also occurs: when you calling `new Array(1)`, you get `[ <1 empty item> ]` instead of expected `[ 1 ]`.
**Instead `new Prray()`, use `Prray.from` or `Prray.of` if you want to create a new prray instance**. Because the class Prray is so compatible with class Array, some "weird" behaviors that exists in `new Array()` can also occurs: when you calling `new Array(1)`, you get `[ <1 empty item> ]` instead of expected `[ 1 ]`.

### Static methods of Class Prray

Expand All @@ -178,7 +162,7 @@ _Compatible with [`Array.from`](https://developer.mozilla.org/en-US/docs/Web/Jav
The Prray.from() method creates a new, shallow-copied Prray instance from an array-like or iterable object.

```javascript
import { Prray } from 'prray'
import Prray from 'prray'

const prr = Prray.from([1, 2, 3, 4])
```
Expand All @@ -190,7 +174,7 @@ _Compatible with [`Array.of`](https://developer.mozilla.org/en-US/docs/Web/JavaS
The Prray.of() method creates a new Prray instance from a variable number of arguments, regardless of number or type of the arguments.

```javascript
import { Prray } from 'prray'
import Prray from 'prray'

const prr = Prray.of(1, 2, 3, 4)
```
Expand All @@ -200,7 +184,7 @@ const prr = Prray.of(1, 2, 3, 4)
The Prray.isArray() method determines whether the passed value is a Prray instance.

```javascript
import { Prray } from 'prray'
import Prray from 'prray'

Prray.isPrray([1, 2, 3]) // false
Prray.isPrray(new Prray(1, 2, 3)) // true
Expand Down Expand Up @@ -243,7 +227,7 @@ The provided async function is called on every element concurrently.
- `concurrency` Number of concurrently pending promises returned by provided function. Default: `Infinity`

```javascript
const urls = prray([
const urls = Prray.from([
/* urls */
])

Expand All @@ -265,7 +249,7 @@ The provided async function is called on every element concurrently.
- `concurrency` Number of concurrently pending promises returned by provided function. Default: `Infinity`

```javascript
const files = prray([
const files = Prray.from([
/* filenames */
])

Expand All @@ -281,7 +265,7 @@ _Think of it as an async version of method `reduce`_
The reduceAsync() method executes a async reducer function (that you provide) on each element of the prray, resulting in a single output value resolved by a promise.

```javascript
const productIds = prray([
const productIds = Prray.from([
/* ids */
])

Expand All @@ -298,7 +282,7 @@ _Think of it as an async version of method `reduceRight`_
The reduceRightAsync() method applies an async function against an accumulator and each value of the prray (from right-to-left) to reduce it to a single value.

```javascript
const productIds = prray([
const productIds = Prray.from([
/* ids */
])

Expand All @@ -315,7 +299,7 @@ _Think of it as an async version of method `find`_
The findAsync() method returns a promise resolved with the first element in the prray that satisfies the provided async testing function.

```javascript
const workers = prray([
const workers = Prray.from([
/* workers */
])

Expand All @@ -329,7 +313,7 @@ _Think of it as an async version of method `findIndex`_
The findIndexAsync() method returns a promise resolved with the index of the first element in the prray that satisfies the provided async testing function. Otherwise, it returns promise resolved with -1, indicating that no element passed the test.

```javascript
const workers = prray([
const workers = Prray.from([
/* workers */
])
const ix = await workers.findIndexAsync(checkHealth)
Expand All @@ -349,7 +333,7 @@ The provided async function is called on every element concurrently.
- `concurrency` Number of concurrently pending promises returned by provided function. Default: `Infinity`

```javascript
const filenames = prray([
const filenames = Prray.from([
/* filenames */
])

Expand All @@ -372,7 +356,7 @@ The provided async function is called on every element concurrently.
- `concurrency` Number of concurrently pending promises returned by provided function. Default: `Infinity`

```javascript
const filenames = prray([
const filenames = Prray.from([
/* filenames */
])

Expand All @@ -389,7 +373,7 @@ _Think of it as an async version of method `sort`_
The sortAsync() method sorts the elements of a prray in place and returns a promise resolved with the sorted prray. The provided function can be an async function that returns a promise resolved with a number.

```javascript
const students = prray([/* ids */])
const students = Prray.from([/* ids */])

const rank = await students.sortAsync((a, b) => {
const scoreA = await getScore(a)
Expand All @@ -409,7 +393,7 @@ The forEachAsync() method executes a provided async function once for each prray
- `concurrency` Number of concurrently pending promises returned by provided function. Default: `Infinity`

```javascript
const emails = prray([
const emails = Prray.from([
/* emails */
])
await emails.forEachAsync(sendAsync)
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
* 2019-10-01
*/

import { Prray, prray } from './prray'
import Prray from './prray'

export { prray, Prray }
export default Prray

module.exports = Prray
module.exports.default = Prray
2 changes: 1 addition & 1 deletion src/methods.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Prray } from './prray'
import Prray from './prray'

export async function mapAsync<T, U>(
arr: Prray<T>,
Expand Down
6 changes: 1 addition & 5 deletions src/prray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as methods from './methods'

// TODO: thisArg

export class Prray<T> extends Array<T> {
export default class Prray<T> extends Array<T> {
static of<T>(...args: T[]): Prray<T> {
return Prray.from(args)
}
Expand Down Expand Up @@ -197,10 +197,6 @@ export class Prray<T> extends Array<T> {
}
}

export function prray<T>(arr: T[]): Prray<T> {
return Prray.from(arr)
}

export function _ensurePrray<T>(arr: T[]): Prray<T> {
if (arr instanceof Prray) {
return arr
Expand Down
2 changes: 1 addition & 1 deletion src/prraypromise.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Prray } from './prray'
import Prray from './prray'

export class PrrayPromise<T> extends Promise<Prray<T>> {
constructor(executor: (resolve: (prray: Prray<T>) => any, reject: (err: Error) => any) => any) {
Expand Down
2 changes: 1 addition & 1 deletion test/_ensurePrray.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava'
import { _ensurePrray, Prray } from '../src/prray'
import Prray, { _ensurePrray } from '../src/prray'

test('_ensurePrray', async t => {
const arr = new Array()
Expand Down
2 changes: 1 addition & 1 deletion test/compatibility.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava'
import { Prray } from '../src/prray'
import Prray from '../src/prray'

test('Compatibility with array: indexing', async t => {
const arr = [1, 2, 3, 4]
Expand Down
26 changes: 13 additions & 13 deletions test/concat.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import test from 'ava'
import { prray } from '../src/prray'
import Prray from '../src/prray'
import { toPrrayPromise } from './test-utils'

test('prray concat', async t => {
t.deepEqual(prray([1, 2]).concat([3, 4]), prray([1, 2, 3, 4]))
t.deepEqual(prray([1, 2]).concat([3, 4], [5, 6]), prray([1, 2, 3, 4, 5, 6]))
t.deepEqual(prray([1, 2]).concat(3, [4, 5]), prray([1, 2, 3, 4, 5]))
t.deepEqual(prray([[1]]).concat([2, [3]] as any), prray([[1], 2, [3]]))
t.deepEqual(prray([[1]]).concat(4 as any, [5, [6]] as any), prray([[1], 4, 5, [6]]))
t.deepEqual(Prray.from([1, 2]).concat([3, 4]), Prray.from([1, 2, 3, 4]))
t.deepEqual(Prray.from([1, 2]).concat([3, 4], [5, 6]), Prray.from([1, 2, 3, 4, 5, 6]))
t.deepEqual(Prray.from([1, 2]).concat(3, [4, 5]), Prray.from([1, 2, 3, 4, 5]))
t.deepEqual(Prray.from([[1]]).concat([2, [3]] as any), Prray.from([[1], 2, [3]]))
t.deepEqual(Prray.from([[1]]).concat(4 as any, [5, [6]] as any), Prray.from([[1], 4, 5, [6]]))

const prr = prray([1, 2])
const prr = Prray.from([1, 2])
t.not(prr.concat([3, 4]), prr) // Immutable
})

test('prraypromise concat', async t => {
t.deepEqual(await toPrrayPromise([1, 2]).concat([3, 4]), prray([1, 2, 3, 4]))
t.deepEqual(await toPrrayPromise([1, 2]).concat([3, 4], [5, 6]), prray([1, 2, 3, 4, 5, 6]))
t.deepEqual(await toPrrayPromise([1, 2]).concat(3, [4, 5]), prray([1, 2, 3, 4, 5]))
t.deepEqual(await toPrrayPromise([[1]]).concat([2, [3]] as any), prray([[1], 2, [3]]))
t.deepEqual(await toPrrayPromise([[1]]).concat(4 as any, [5, [6]] as any), prray([[1], 4, 5, [6]]))
t.deepEqual(await toPrrayPromise([1, 2]).concat([3, 4]), Prray.from([1, 2, 3, 4]))
t.deepEqual(await toPrrayPromise([1, 2]).concat([3, 4], [5, 6]), Prray.from([1, 2, 3, 4, 5, 6]))
t.deepEqual(await toPrrayPromise([1, 2]).concat(3, [4, 5]), Prray.from([1, 2, 3, 4, 5]))
t.deepEqual(await toPrrayPromise([[1]]).concat([2, [3]] as any), Prray.from([[1], 2, [3]]))
t.deepEqual(await toPrrayPromise([[1]]).concat(4 as any, [5, [6]] as any), Prray.from([[1], 4, 5, [6]]))

const prr = prray([1, 2])
const prr = Prray.from([1, 2])
t.not(await toPrrayPromise(prr).concat([3, 4]), prr) // Immutable
})
22 changes: 11 additions & 11 deletions test/copyWithin.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import test from 'ava'
import { prray } from '../src/prray'
import Prray from '../src/prray'
import { toPrrayPromise } from './test-utils'

test('prray copyWithin', async t => {
const arr = [1, 2, 3, 4, 5]
const prr = prray([1, 2, 3, 4, 5])
const prr = Prray.from([1, 2, 3, 4, 5])

t.is(prr.copyWithin(-2, 0), prr)

t.deepEqual(prr.copyWithin(-2, 0), prray(arr.copyWithin(-2, 0)))
t.deepEqual(prr.copyWithin(0, 3), prray(arr.copyWithin(0, 3)))
t.deepEqual(prr.copyWithin(0, 3, 4), prray(arr.copyWithin(0, 3, 4)))
t.deepEqual(prr.copyWithin(-2, -3, -1), prray(arr.copyWithin(-2, -3, -1)))
t.deepEqual(prr.copyWithin(-2, 0), Prray.from(arr.copyWithin(-2, 0)))
t.deepEqual(prr.copyWithin(0, 3), Prray.from(arr.copyWithin(0, 3)))
t.deepEqual(prr.copyWithin(0, 3, 4), Prray.from(arr.copyWithin(0, 3, 4)))
t.deepEqual(prr.copyWithin(-2, -3, -1), Prray.from(arr.copyWithin(-2, -3, -1)))

t.is(prr.copyWithin(-2, -3, -1), prr) // mutable
})

test('prraypromise copyWithin', async t => {
const arr = [1, 2, 3, 4, 5]
const prr = prray([1, 2, 3, 4, 5])
const prr = Prray.from([1, 2, 3, 4, 5])

t.is(await toPrrayPromise(prr).copyWithin(-2, 0), prr)

t.deepEqual(await toPrrayPromise(prr).copyWithin(-2, 0), prray(arr.copyWithin(-2, 0)))
t.deepEqual(await toPrrayPromise(prr).copyWithin(0, 3), prray(arr.copyWithin(0, 3)))
t.deepEqual(await toPrrayPromise(prr).copyWithin(0, 3, 4), prray(arr.copyWithin(0, 3, 4)))
t.deepEqual(await toPrrayPromise(prr).copyWithin(-2, -3, -1), prray(arr.copyWithin(-2, -3, -1)))
t.deepEqual(await toPrrayPromise(prr).copyWithin(-2, 0), Prray.from(arr.copyWithin(-2, 0)))
t.deepEqual(await toPrrayPromise(prr).copyWithin(0, 3), Prray.from(arr.copyWithin(0, 3)))
t.deepEqual(await toPrrayPromise(prr).copyWithin(0, 3, 4), Prray.from(arr.copyWithin(0, 3, 4)))
t.deepEqual(await toPrrayPromise(prr).copyWithin(-2, -3, -1), Prray.from(arr.copyWithin(-2, -3, -1)))

t.is(await toPrrayPromise(prr).copyWithin(-2, -3, -1), prr) // mutable
})
4 changes: 2 additions & 2 deletions test/entries.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import test from 'ava'
import { prray } from '../src/prray'
import Prray from '../src/prray'
import { toPrrayPromise } from './test-utils'

const arr = ['a', 'b', 'c', 'd']
const p = prray(arr)
const p = Prray.from(arr)
const pp = toPrrayPromise(arr)

test('prray entries', async t => {
Expand Down
14 changes: 7 additions & 7 deletions test/every.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import test from 'ava'
import * as sinon from 'sinon'
import { prray } from '../src/prray'
import Prray from '../src/prray'
import { toPrrayPromise, isEven } from './test-utils'

test('prray every', async t => {
t.is(prray([2, 4, 6]).every(isEven), true)
t.is(prray([1, 2, 3]).every(isEven), false)
t.is(Prray.from([2, 4, 6]).every(isEven), true)
t.is(Prray.from([1, 2, 3]).every(isEven), false)
})

test('prraypromise every', async t => {
Expand All @@ -15,7 +15,7 @@ test('prraypromise every', async t => {

test('prray every compatibility 1', async t => {
const func = sinon.fake(() => true)
const prr = prray(['a', 'b', 'c'])
const prr = Prray.from(['a', 'b', 'c'])
prr.every(func)

t.is(func.called, true)
Expand All @@ -36,15 +36,15 @@ test('prray every compatibility 1', async t => {

test('prray every compatibility 2', async t => {
const func = sinon.fake(() => false)
const prr = prray(['a', 'b', 'c'])
const prr = Prray.from(['a', 'b', 'c'])
prr.every(func)

t.is(func.callCount, 1)
})

test('prraypromise every compatibility 1', async t => {
const func = sinon.fake(() => true)
const prr = prray(['a', 'b', 'c'])
const prr = Prray.from(['a', 'b', 'c'])
const pp = toPrrayPromise(prr)
await pp.every(func)

Expand All @@ -66,7 +66,7 @@ test('prraypromise every compatibility 1', async t => {

test('prraypromise every compatibility 2', async t => {
const func = sinon.fake(() => false)
const prr = prray(['a', 'b', 'c'])
const prr = Prray.from(['a', 'b', 'c'])
const pp = toPrrayPromise(prr)
await pp.every(func)

Expand Down
Loading

0 comments on commit 79d51d2

Please sign in to comment.