Skip to content

Commit

Permalink
BREAKING: Refactor project into individual modules per type
Browse files Browse the repository at this point in the history
* Removes camel-cased card key names
* Fixes Mastercard casing (previously MasterCard)
* Adds instructions for including custom types
  • Loading branch information
bendrucker committed Dec 18, 2017
1 parent 7ea9490 commit 54c195d
Show file tree
Hide file tree
Showing 37 changed files with 459 additions and 344 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
28 changes: 1 addition & 27 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,28 +1,2 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# Commenting this out is preferred by some people, see
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

# Users Environment Variables
.lock-wscript
package-lock.json
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
language: node_js
sudo: false
node_js:
- '5'
- '4'
- '6'
- '8'
11 changes: 1 addition & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
'use strict'

var types = exports.types = require('./src/types')
exports.Type = require('./src/type')

exports.find = function findCardType (callback) {
for (var typeName in types) {
var type = types[typeName]
var result = callback(type)
if (result) return type
}
}
module.exports = require('./types')
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Card type definitions and methods for creditcards",
"main": "index.js",
"scripts": {
"test": "standard && tape test.js | tap-spec"
"test": "standard && tape test/*.js | tap-spec"
},
"repository": {
"type": "git",
Expand All @@ -25,13 +25,13 @@
"xtend": "~4.0.0"
},
"devDependencies": {
"standard": "^8.0.0",
"run-parallel": "~1.1.6",
"standard": "^10.0.3",
"tap-spec": "^4.0.0",
"tape": "^4.0.0"
},
"files": [
"index.js",
"src/",
"readme.md"
"*.js",
"types/"
]
}
56 changes: 31 additions & 25 deletions README.md → readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
## Card Types

* Visa
* MasterCard
* Mastercard
* American Express
* Diners Club
* Discover
Expand All @@ -16,47 +16,54 @@
* Dankort
* Troy

Visa Electron cards will validate and match as regular Visa cards.
Visa Electron cards will validate and match as regular Visa cards.

[Open an issue](https://github.com/bendrucker/creditcards-types/issues/new) if you need a type that's missing.
Card data can be required individually by [type](types/). The main module includes _all_ defined card types. You may want to select specific cards that your customers will use to save bytes or avoid confusion.

[Open an issue](https://github.com/bendrucker/creditcards-types/issues/new) or a PR if you'd like to contribute documentation/code for a type that's missing.

## Installing

```sh
npm install creditcards-types
npm install --save creditcards-types
```

## Usage

```js
var types = require('creditcards-types').types
var visa = types.visa
visa.test('4242424242424242') // true
```

## API
// finding
var types = require('creditcards-types')
var type = types.find(type => type.test('4', true))
// type.name => Visa

#### `find(callback)` -> `type` / `undefined`

Iterates through the available types until the `callback` returns a truthy value. Returns the first matching type. Iteration order is undefined.
// specific types
var visa = require('creditcards-types/types/visa')
visa.test('4242424242424242') // true

##### callback
// creating custom types
var Type = require('creditcards-types/type')
var myCard = Type({
name: 'My Card',
pattern: /^999\d{13}$/
eagerPattern: /^999/,
luhn: false
})

*Required*
Type: `function`
var myTypes = types.concat(myCard) // myCard gets lowest priority
```

Callback that is called with card type objects and should return truthy/falsy until a match is found.
## API

#### `new Type(config)` -> `type`
#### `new Type(data)` -> `type`

Creates a new card type.

```js
var Type = require('creditcards-types').Type
var type = new Type(config)
var Type = require('creditcards-types/type')
var type = Type(data)
```

##### config
##### data

*Required*
Type: `object`
Expand Down Expand Up @@ -105,14 +112,13 @@ Default: `false`
When `false`, the full card pattern is used. When `true`, the eager pattern is tested instead.

```js
var types = require('creditcards-types').types;
var visa = types.visa;
var visa = require('creditcards-types/types/visa')

// Strict type validation
visa.test('4242424242424242'); // => true
visa.test('4242424242424242') // => true

// Eager type checking
visa.test('42', true); // => true
visa.test('42', true) // => true
```

---
Expand Down
67 changes: 0 additions & 67 deletions src/types.js

This file was deleted.

Loading

0 comments on commit 54c195d

Please sign in to comment.