Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wtgtybhertgeghgtwtg committed Apr 19, 2018
0 parents commit 8fbf483
Show file tree
Hide file tree
Showing 22 changed files with 1,038 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
modules: process.env.NODE_ENV === 'test' && 'commonjs',
targets: {node: '4'},
},
],
'@babel/preset-flow',
],
};
88 changes: 88 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
jobs:

lint:
docker:
- image: circleci/node:8
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
- v1-dependencies-
- run: yarn install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- run: yarn lint
working_directory: ~/repo

release:
docker:
- image: circleci/node:8
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
- v1-dependencies-
- run: yarn install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- run: yarn build
- run: npx semantic-release

test:
docker:
- image: circleci/node:8
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
- v1-dependencies-
- run: yarn install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- run: yarn test:src
working_directory: ~/repo

test-dist:
docker:
- image: circleci/node:8
steps:
- checkout
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
- v1-dependencies-
- run: yarn install
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- run: yarn build
- run: yarn test:dist
working_directory: ~/repo

version: 2

workflows:
test_and_release:
jobs:
- lint
- test
- test-dist
- release:
filters:
branches:
only: master
requires:
- lint
- test
- test-dist
version: 2
2 changes: 2 additions & 0 deletions .commitlintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extends:
- '@commitlint/config-conventional'
4 changes: 4 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coverage/*
dist/*
flow-typed/*
node_modules/*
19 changes: 19 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
env:
jest: true

extends:
- airbnb-base
- plugin:flowtype/recommended
- prettier
- prettier/flowtype

parser: babel-eslint

plugins:
- flowtype

rules:
import/no-extraneous-dependencies:
- error
- devDependencies:
- scripts/**
11 changes: 11 additions & 0 deletions .flowconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[ignore]

[include]

[libs]

[lints]

[options]

[strict]
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
coverage
dist
node_modules
yarn.lock
yarn-debug.log*
yarn-error.log*
3 changes: 3 additions & 0 deletions .huskyrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hooks:
commit-msg: commitlint -e $HUSKY_GIT_PARAMS
pre-commit: flow && lint-staged
3 changes: 3 additions & 0 deletions .lintstagedrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters:
"*.{json,md}": ["prettier --write", "git add"]
"*.{js}": ["prettier --write", "eslint --fix", "git add", "jest --config ./scripts/jest/config.source.js --bail --findRelatedTests"]
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package.json
3 changes: 3 additions & 0 deletions .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bracketSpacing: false
singleQuote: true
trailingComma: all
9 changes: 9 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) Matthew Fernando Garcia <[email protected]>

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.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# sole-entry

> Assert a Map has a given key and return the value for it.
Pretty much just `map.get(key)`, but throws if `key` isn't there.

## Install

```
$ yarn add get-certain
```

## Usage

```js
const getCertain = require('get-certain');

const map = new Map();

map.set('foo', 1);

// Returns 1.
getCertain(map, 'foo');

// This throws.
getCertain(map, 'bar');

// This throws with a custom message.
getCertain(map, 'bar', 'This map is barless.');
```

## API

### getCertain(map, key, [message])

Returns the value from `map` for `key`, or throws if it is not there.

#### map

* Type: `Map`

A Map. I hope you know what you're putting in there.

#### key

* Type: `any`

A key that is hopefully in `map`.

#### message

* Type: `string`

An optional message to throw. Defaults to '"map" does not have an entry for "key".', which isn't terribly descriptive.

## License

MIT © Matthew Fernando Garcia
39 changes: 39 additions & 0 deletions __tests__/getCertain.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// flow
import getCertain from '../src';

describe('getCertain', () => {
describe('invariants', () => {
it('throws if "map" is undefined.', () => {
expect(() => getCertain()).toThrow('"map" must be a Map.');
});
it('throws if "map" is not a Map-like object.', () => {
expect(() => getCertain({has: 3})).toThrow('"map" must be a Map.');
});
it('throws if "message" is not a string or undefined.', () => {
expect(() => getCertain(new Map(), 1, 1)).toThrow(
'"message" must be a string or undefined.',
);
});
});

it('returns the value for the given key.', () => {
const map = new Map();
const key = 'Some key.';
const value = 'Some value.';
map.set(key, value);
expect(getCertain(map, key)).toEqual(value);
});

it('throws with the default message if no "message" is provided and "key" is not present.', () => {
const map = new Map();
expect(() => getCertain(map, 'Some key.')).toThrow(
'"map" does not have an entry for "key".',
);
});

it('throws with "message" if provided and "key" is not present.', () => {
const map = new Map();
const message = 'Some custom message.';
expect(() => getCertain(map, 'Some key.', message)).toThrow(message);
});
});
Loading

0 comments on commit 8fbf483

Please sign in to comment.