Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlucas committed Mar 13, 2016
0 parents commit 71cfb93
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.nyc_output
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js
node_js:
- "4"
- "5"
- "iojs"
- "iojs-v1"
- "iojs-v2"
sudo: false
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Evan Lucas

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# map-util

[![Build Status](https://travis-ci.org/evanlucas/map-util.svg)](https://travis-ci.org/evanlucas/map-util)
[![Coverage Status](https://coveralls.io/repos/evanlucas/map-util/badge.svg?branch=master&service=github)](https://coveralls.io/github/evanlucas/map-util?branch=master)

Simple set of utilities that make getting the previous or next value in a map
easier.

## Install

```bash
$ npm install [--save] map-util
```

## Usage

```js
'use strict'

const nextVal = require('map-util').nextVal
const prevVal = require('map-util').prevVal

const map = new Map()
map.set('1', '1')
map.set('2', '2')

nextVal('1', map) // => '2'
nextVal('2', map) // => null
// If we want to wrap around:
nextVal('2', map, true) // => '1'

prevVal('2', map) // => '1'
prevVal('1', map) // => null
// If we want to wrap around:
prevVal('1', map, true) // => '2'
```

## Test

```bash
$ npm test
```

## Author

Evan Lucas

## License

MIT (See `LICENSE` for more info)
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
'use strict'

exports.nextVal = require('./lib/next-val')
exports.prevVal = require('./lib/prev-val')
34 changes: 34 additions & 0 deletions lib/next-val.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

module.exports = nextVal

function nextVal(search, map, wrap) {
if (!(map instanceof Map)) {
throw new TypeError('map must be a Map')
}

if (map.size < 2) {
return null
}

let found = false

for (const item of map.values()) {
if (found) {
return item
}

if (search === item) {
found = true
}
}

// this means we tried to get the next value of the last value,
// so return the first
if (wrap && found) {
return map.values().next().value
}

// could not find item
return null
}
40 changes: 40 additions & 0 deletions lib/prev-val.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

module.exports = prevVal

const lastSymbol = Symbol('last')

function prevVal(search, map, wrap) {
if (!(map instanceof Map)) {
throw new TypeError('map must be a Map')
}

if (map.size < 2) {
return null
}

let prev = null
let last = null

for (const item of map.values()) {
if (item === search) {
if (prev === null) {
last = lastSymbol
continue
}
return prev
}

prev = item
if (last === lastSymbol) {
last = item
}
}

if (wrap && last) {
return last
}


return null
}
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "map-util",
"version": "1.0.0",
"description": "Utilities that make getting prev/next values in a map easier",
"main": "index.js",
"scripts": {
"test": "tap test.js --cov"
},
"dependencies": {},
"devDependencies": {
"tap": "~5.7.0"
},
"license": "MIT",
"author": "Evan Lucas <evanlucas@me.com>",
"repository": {
"type": "git",
"url": "https://github.com/evanlucas/map-util"
},
"homepage": "https://github.com/evanlucas/map-util",
"bugs": {
"url": "https://github.com/evanlucas/map-util/issues"
}
}
40 changes: 40 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

const test = require('tap').test
const utils = require('./')

test('nextVal', (t) => {
t.throws(function() {
t.equal(utils.nextVal())
}, /map must be a Map/)

const m = new Map()
t.equal(utils.nextVal(null, m), null)
m.set('1', '1')
t.equal(utils.nextVal(null, m), null)

m.set('2', '2')
t.equal(utils.nextVal('2', m), null)
t.equal(utils.nextVal('2', m, true), '1')
t.equal(utils.nextVal('1', m), '2')
t.equal(utils.nextVal('3', m), null)
t.end()
})

test('prevVal', (t) => {
t.throws(function() {
t.equal(utils.prevVal())
}, /map must be a Map/)

const m = new Map()
t.equal(utils.prevVal(null, m), null)
m.set('1', '1')
t.equal(utils.prevVal(null, m), null)

m.set('2', '2')
t.equal(utils.prevVal('2', m), '1')
t.equal(utils.prevVal('1', m, true), '2')
t.equal(utils.prevVal('1', m), null)
t.equal(utils.prevVal('3', m), null)
t.end()
})

0 comments on commit 71cfb93

Please sign in to comment.