Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
colinmeinke committed Nov 26, 2016
0 parents commit 531e99c
Show file tree
Hide file tree
Showing 15 changed files with 3,993 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dist/
lib/
node_modules/
yarn.error
yarn-error.log
8 changes: 8 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.gitignore
.travis.yml
rollup.config.js
src/
test/
yarn.error
yarn.lock
yarn-error.log
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sudo: false
language: node_js
cache:
directories:
- node_modules
notifications:
email: false
node_js:
- '6'
before_install:
- npm i -g npm@^3.0.0
before_script:
- npm prune
script:
- npm run lint
- npm test
after_success:
- npm run semantic-release
branches:
except:
- /^v\d+\.\d+\.\d+$/
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Array Combos

A function to get the unique item combinations from an array

- Plain old vanilla JS
- Just 0.6kb gzipped
- Does **not** use generators

## Installation

```
npm install array-combos
```

## Usage

```js
import combos from 'array-combos'

const arr = [ 1, 2, 3 ]

console.log(combos(arr))

// [
// [],
// [ 1 ],
// [ 2 ],
// [ 3 ],
// [ 1, 2 ],
// [ 1, 3 ],
// [ 2, 3 ],
// [ 1, 2, 3 ]
// ]
```

An optional second argument will restrict results
to combinations with that number of items.

```js
import combos from 'array-combos'

const arr = [ 1, 2, 3 ]

console.log(combos(arr, 2))

// [
// [ 1, 2 ],
// [ 1, 3 ],
// [ 2, 3 ],
// ]
```

## Browser support

Array Combos is packaged with Babel, and
[makes use of `Array.from`](https://babeljs.io/docs/usage/caveats).
If you want Array Combos to work on browsers that don't support
this method (e.g. IE11), then you will need to
[polyfill `Array.from`](https://github.com/zloirock/core-js)
before using `combos`.
15 changes: 15 additions & 0 deletions __tests__/can-move.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* globals test, expect */

import canMove from '../src/can-move'

test('Cannot move when no gap between marker positions', () => {
expect(canMove(0, 1)).toBe(false)
})

test('Can move when gap of one between marker positions', () => {
expect(canMove(0, 2)).toBe(true)
})

test('Can move when multiple gap between marker positions', () => {
expect(canMove(5, 8)).toBe(true)
})
147 changes: 147 additions & 0 deletions __tests__/combo.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/* globals test, expect */

import combos from '../src'

test('An empty array returns one combination', () => {
expect(combos([])).toEqual([
[]
])
})

test('An array with one item returns two combinations', () => {
expect(combos([ 1 ])).toEqual([
[],
[ 1 ]
])
})

test('An array with two items returns four combinations', () => {
expect(combos([ 1, 2 ])).toEqual([
[],
[ 1 ],
[ 2 ],
[ 1, 2 ]
])
})

test('An array with three items returns eight combinations', () => {
expect(combos([ 1, 2, 3 ])).toEqual([
[],
[ 1 ],
[ 2 ],
[ 3 ],
[ 1, 2 ],
[ 1, 3 ],
[ 2, 3 ],
[ 1, 2, 3 ]
])
})

test('An array with four items returns sixteen combinations', () => {
expect(combos([ 1, 2, 3, 4 ])).toEqual([
[],
[ 1 ],
[ 2 ],
[ 3 ],
[ 4 ],
[ 1, 2 ],
[ 1, 3 ],
[ 1, 4 ],
[ 2, 3 ],
[ 2, 4 ],
[ 3, 4 ],
[ 1, 2, 3 ],
[ 1, 2, 4 ],
[ 1, 3, 4 ],
[ 2, 3, 4 ],
[ 1, 2, 3, 4 ]
])
})

test('An array with five items returns thirty two combinations', () => {
expect(combos([ 1, 2, 3, 4, 5 ])).toEqual([
[],
[ 1 ],
[ 2 ],
[ 3 ],
[ 4 ],
[ 5 ],
[ 1, 2 ],
[ 1, 3 ],
[ 1, 4 ],
[ 1, 5 ],
[ 2, 3 ],
[ 2, 4 ],
[ 2, 5 ],
[ 3, 4 ],
[ 3, 5 ],
[ 4, 5 ],
[ 1, 2, 3 ],
[ 1, 2, 4 ],
[ 1, 2, 5 ],
[ 1, 3, 4 ],
[ 1, 3, 5 ],
[ 1, 4, 5 ],
[ 2, 3, 4 ],
[ 2, 3, 5 ],
[ 2, 4, 5 ],
[ 3, 4, 5 ],
[ 1, 2, 3, 4 ],
[ 1, 2, 3, 5 ],
[ 1, 2, 4, 5 ],
[ 1, 3, 4, 5 ],
[ 2, 3, 4, 5 ],
[ 1, 2, 3, 4, 5 ]
])
})

test('An array with four items returns six two item combinations', () => {
expect(combos([ 1, 2, 3, 4 ], 2)).toEqual([
[ 1, 2 ],
[ 1, 3 ],
[ 1, 4 ],
[ 2, 3 ],
[ 2, 4 ],
[ 3, 4 ]
])
})

test('An array with five items returns ten three items combinations', () => {
expect(combos([ 1, 2, 3, 4, 5 ], 3)).toEqual([
[ 1, 2, 3 ],
[ 1, 2, 4 ],
[ 1, 2, 5 ],
[ 1, 3, 4 ],
[ 1, 3, 5 ],
[ 1, 4, 5 ],
[ 2, 3, 4 ],
[ 2, 3, 5 ],
[ 2, 4, 5 ],
[ 3, 4, 5 ]
])
})

test('An array with six items returns twenty three items combinations', () => {
expect(combos([ 1, 2, 3, 4, 5, 6 ], 3)).toEqual([
[ 1, 2, 3 ],
[ 1, 2, 4 ],
[ 1, 2, 5 ],
[ 1, 2, 6 ],
[ 1, 3, 4 ],
[ 1, 3, 5 ],
[ 1, 3, 6 ],
[ 1, 4, 5 ],
[ 1, 4, 6 ],
[ 1, 5, 6 ],
[ 2, 3, 4 ],
[ 2, 3, 5 ],
[ 2, 3, 6 ],
[ 2, 4, 5 ],
[ 2, 4, 6 ],
[ 2, 5, 6 ],
[ 3, 4, 5 ],
[ 3, 4, 6 ],
[ 3, 5, 6 ],
[ 4, 5, 6 ]
])
})
51 changes: 51 additions & 0 deletions __tests__/last-movable.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* globals test, expect */

import lastMovable from '../src/last-movable'

test('Returns null when one item in only position', () => {
expect(lastMovable([ 0 ], 1)).toBe(null)
})

test('Returns null when one item in last position', () => {
expect(lastMovable([ 1 ], 2)).toBe(null)
})

test('Returns null when three items and in only positions', () => {
expect(lastMovable([ 0, 1, 2 ], 3)).toBe(null)
})

test('Returns null when three items are in last positions', () => {
expect(lastMovable([ 3, 4, 5 ], 6)).toBe(null)
})

test('Returns correct index when one item can move', () => {
expect(lastMovable([ 0 ], 2)).toBe(0)
})

test('Returns correct index when one item can move', () => {
expect(lastMovable([ 3 ], 6)).toBe(0)
})

test('Returns correct index when last item can move', () => {
expect(lastMovable([ 0, 1 ], 3)).toBe(1)
})

test('Returns correct index when last item can move', () => {
expect(lastMovable([ 0, 1, 2, 6 ], 10)).toBe(3)
})

test('Returns correct index when last but one item can move', () => {
expect(lastMovable([ 0, 1, 4 ], 5)).toBe(1)
})

test('Returns correct index when last but one item can move', () => {
expect(lastMovable([ 0, 5, 6, 9 ], 10)).toBe(2)
})

test('Returns correct index when last but two item can move', () => {
expect(lastMovable([ 0, 1, 3, 4 ], 5)).toBe(1)
})

test('Returns correct index when last but two item can move', () => {
expect(lastMovable([ 0, 4, 5, 8, 9 ], 10)).toBe(2)
})
27 changes: 27 additions & 0 deletions __tests__/positons.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* globals test, expect */

import positions from '../src/positions'

test('Positions returns correct items when created with an integer (1)', () => {
expect(positions(1)).toEqual([ 0 ])
})

test('Positions returns correct items when created with an integer (2)', () => {
expect(positions(2)).toEqual([ 0, 1 ])
})

test('Positions returns correct items when created with an integer (3)', () => {
expect(positions(3)).toEqual([ 0, 1, 2 ])
})

test('Positions returns identicle items when created with an array', () => {
expect(positions([ 2, 3, 4 ])).toEqual([ 2, 3, 4 ])
})

test('Positions returns correct items when passed an adjust argument', () => {
expect(positions([ 0, 3, 6 ], { i: 1, p: 4 })).toEqual([ 0, 4, 5 ])
})

test('Positions returns correct items when passed an adjust argument', () => {
expect(positions([ 2, 3, 6 ], { i: 1, p: 4 })).toEqual([ 2, 4, 5 ])
})
Loading

0 comments on commit 531e99c

Please sign in to comment.