Skip to content

Commit 90c3b37

Browse files
committed
electron fixes
1 parent c9b433d commit 90c3b37

File tree

3 files changed

+67
-23
lines changed

3 files changed

+67
-23
lines changed

README.md

+46-7
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ Usage
1313
-----
1414

1515
require the library
16-
16+
1717
var Swipe = require('card-swipe');
18-
18+
1919
the simplest way to get it running is to use the built-in stdio hook to get it running from the terminal:
2020

2121
Swipe.stdIn()
2222
new Swipe(function(swipeData){
2323
console.log('swipe', swipeData);
2424
});
25-
25+
2626

2727
this is the shorthand for:
2828

@@ -40,21 +40,60 @@ this is the shorthand for:
4040
for(var lcv=0; lcv < chunk.length; lcv++) scanner.input(chunk[lcv]);
4141
if (key && key.ctrl && key.name == 'c') process.exit();
4242
});
43-
43+
4444
likely if you are integrating this into an app, stdin is not going to be good enough for you... but luckily the scanner will wire up to just about anything.
4545

4646
Additionally, so I could test these things out I built a generator function
4747

4848
Swipe.generate(field, [values])
49-
49+
5050
which can generate luhn and bin valid account numbers, track_one and track_two data (since you can't really be saving these things, and test cards are continually expiring).
5151

5252
and for my testing harness
5353

5454
Swipe.fake(scanner)
55-
55+
5656
which generates a random fake swipe across the passed in scanner.
5757

58+
Browser Example - Vue Component
59+
-------------------------------
60+
61+
```html
62+
<template>
63+
<div class="swipe">
64+
<slot></slot>
65+
</div>
66+
</template>
67+
68+
<script>
69+
import * as Swipe from 'card-swipe';
70+
71+
export default {
72+
name: 'CardSwipe',
73+
methods : {
74+
handleSwipe : function(swipe){
75+
console.log('CHAR', swipe);
76+
}
77+
},
78+
created: function(){
79+
window.addEventListener('keydown', (e)=>{
80+
if(e.key.length === 1) scanner.input(e.key);
81+
//if(e.key === 'Enter') scanner.input("\n");
82+
});
83+
let scanner = new Swipe.Scanner();
84+
new Swipe({
85+
scanner : scanner,
86+
onScan : (swipeData)=>{
87+
this.handleSwipe(swipeData);
88+
console.log('swipe', swipeData);
89+
}
90+
});
91+
}
92+
}
93+
</script>
94+
<style scoped></style>
95+
```
96+
5897
Testing
5998
-------
6099
Tests use mocha/should to execute the tests from root
@@ -67,4 +106,4 @@ Right now this only supports credit cards, but this could easily expand to gift
67106

68107
Enjoy,
69108

70-
-Abbey Hawk Sparrow
109+
-Abbey Hawk Sparrow

card-swipe.js

+20-15
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,22 @@ var CreditSwipe = function(options){
7474
return str.match(/%B[0-9 ]{13,18}\^[\/A-Z ]+\^[0-9]{13,}\?/mi) || str.match(/;[0-9]{13,16}=[0-9]{13,}\?/mi);
7575
}
7676
});
77-
scanner.on('credit-swipe', function(result){
77+
var swipes = {};
78+
var finalSwipe = function(results, cb){
79+
if(!swipes[results.account]) swipes[results.account] = results;
80+
else{
81+
Object.keys(results).forEach((key)=>{
82+
swipes[results.account][key] = results[key];
83+
});
84+
}
85+
setTimeout(()=>{
86+
let swipe = swipes[results.account];
87+
delete swipes[results.account];
88+
if(swipe) cb(swipe)
89+
}, 200);
90+
}
91+
scanner.on('credit-swipe', function(res){
92+
var result = res[0] || res;
7893
var results = {};
7994
var something = false;
8095
if(result.substring(0,1) == '%'){
@@ -117,20 +132,10 @@ var CreditSwipe = function(options){
117132
if(options.luhn){
118133
results['valid'] = require("luhn").validate(results.account);
119134
}
120-
res.push(results);
121-
setTimeout(function(){
122-
var results = res.shift() || {};
123-
res.forEach(function(item){
124-
Object.keys(item).forEach(function(fieldName){
125-
if(!results[fieldName]) results[fieldName] = item[fieldName];
126-
});
127-
});
128-
res = [];
129-
callback(results);
130-
}, 50); //allow for 50ms of latency for a full scan
135+
finalSwipe(results, callback);
131136
});
132137
};
133-
138+
134139
Keyboard.Sequence = {};
135140

136141
Keyboard.Sequence.types = require('./card_type');
@@ -141,7 +146,7 @@ Object.keys(Keyboard.Sequence.types).forEach(function(stringKey){
141146
Keyboard.Sequence.types = intKeys;
142147

143148
//todo: switch to new format:
144-
// Issuer Name [Card Name]<Network>|country|{details}
149+
// Issuer Name [Card Name]<Network>|country|{details}
145150
Keyboard.Sequence.issuers = require('./issuer_data');
146151

147152
module.exports = CreditSwipe;
@@ -187,7 +192,7 @@ module.exports.generate = function(type, options){
187192
get('track_two')
188193
]
189194
default : return 'blah';
190-
195+
191196
}
192197
};
193198
module.exports.stdIn = function(){

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "card-swipe",
33
"homepage": "https://github.com/khrome/card-swipe",
4-
"version": "1.0.3",
4+
"version": "1.1.0",
55
"main": "card-swipe.js",
66
"scripts": {
77
"test": " ./node_modules/mocha/bin/mocha"

0 commit comments

Comments
 (0)