Skip to content

Commit

Permalink
Cleaned, and added some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
omarshammas committed Jul 21, 2013
1 parent dda9e9c commit d51cceb
Show file tree
Hide file tree
Showing 30 changed files with 2,022 additions and 1,907 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
*.DS_Store
lib/*
67 changes: 46 additions & 21 deletions Cakefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
{print} = require 'util'
{spawn} = require 'child_process'

task 'build', 'Build lib/ from src/', ->
coffee = spawn 'coffee', ['-c', '-o', 'lib', 'src']
coffee.stderr.on 'data', (data) ->
process.stderr.write data.toString()
coffee.stdout.on 'data', (data) ->
print data.toString()
coffee.on 'exit', (code) ->
callback?() if code is 0
# Adapted from http://kgn.github.io/wheel.coffee/Cakefile.html

{spawn, exec} = require 'child_process'
{log, error} = console; print = log
fs = require 'fs'

run = (name, args...) ->
proc = spawn(name, args)
proc.stdout.on 'data', (buffer) -> print buffer if buffer = buffer.toString().trim()
proc.stderr.on 'data', (buffer) -> error buffer if buffer = buffer.toString().trim()
proc.on 'exit', (status) -> process.exit(1) if status isnt 0

shell = (cmds, callback) ->
cmds = [cmds] if Object::toString.apply(cmds) isnt '[object Array]'
exec cmds.join(' && '), (err, stdout, stderr) ->
print trimStdout if trimStdout = stdout.trim()
error stderr.trim() if err
callback() if callback


# task 'coffee', 'Build lib/ from src/', ->
# run 'coffee', '-co', 'lib', 'src'

task 'coffee', 'Builds lib/jquery.formance.js from src/', ->
run 'coffee', '-j', 'lib/jquery.formance.js', '-c', 'src'

task 'watch', 'Watch src/ for changes', ->
coffee = spawn 'coffee', ['-w', '-c', '-o', 'lib', 'src']
coffee.stderr.on 'data', (data) ->
process.stderr.write data.toString()
coffee.stdout.on 'data', (data) ->
print data.toString()
run 'coffee', '-wj', 'lib/jquery.formance.js', '-c', 'src'

task 'test', 'Run tests', ->
mocha = spawn 'mocha', ['--compilers', 'coffee:coffee-script']
mocha.stderr.on 'data', (data) ->
process.stderr.write data.toString()
mocha.stdout.on 'data', (data) ->
print data.toString()
run 'mocha', '--recursive', '--compilers', 'coffee:coffee-script', '-c'

task 'minify', 'Minifies any js file in the lib/ folder.', ->
dir = 'lib'
fs.readdir dir, (err, files) ->
throw err if err
for file in files
continue if not /\.js$/.test(file) or /\.min\.js$/.test(file)

nameparts = file.split '.'
nameparts[nameparts.length-1] = 'min'
nameparts.push 'js'
newname = nameparts.join '.'
print "minify: #{file} -> #{newname}"

shell "uglifyjs --output #{dir}/#{newname} #{dir}/#{file}"

task 'build', 'Builds lib/jquery.formance.js and minifies it.', ->
invoke 'coffee'
invoke 'minify'
3 changes: 2 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Copyright (c) 2012 Stripe ([email protected])
Copyright (c) 2013 Omar Shammas ([email protected])
and Stripe ([email protected])

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
49 changes: 26 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
# jQuery.formance

A general purpose library for formatting and validating form fields, based on Stripe's jQuery.payment library.

## Getting Started

## Installation
Coming soon, in the mean time checkout the example.

```
npm install jquery
npm install mocha
npm install coffeescript
npm install https://github.com/omarshammas/jquery.formance.git
```
## Example

Make sure your node path is setup correctly.
Look in ```./example/index.html```

```
export NODE_PATH=/usr/local/lib/node_modules
```
## Adding A New Field

## Cake
To can create your own formatters and validators, simply add a new file under ```src/zfields/``` with the name of the field type. It is probably best to look at how the other fields were written before starting your own. You can even use them as a base.

Cake is the equivalent of make and rake for coffeescript, and accepts the following commands.
It is important to write tests for the new field to ensure it works as expected. Each field should have a corresponding file under ```test/fields/```.

## Set up Project Locally

### Installation

```
cake build
cake watch
cake test
npm install -g jquery
npm install -g mocha
npm install -g coffee-script
npm install -g uglify-js
npm install https://github.com/omarshammas/jquery.formance.git
```

## Compiling
### Cake

You can compile the coffeescript files into a single file (formance.js) using
Cake is the equivalent of make and rake for coffeescript, and provides the following tasks.

```
coffee --join lib/formance.js --compile src/*
cake coffee # Builds lib/jquery.formance.js from src/
cake watch # Watch src/ for changes
cake test # Runs all tests
cake minify # Minifies any js files in lib/
cake build # Builds lib/jquery.formance.js and minifies it
```

### Side Notes

## Side Notes

src/fields was renamed to src/zfields to ensure that jquery.formance.coffee was compiled first otherwise the fields wouldn't be able to access $.fn.formance.

```src/fields``` was renamed to ```src/zfields``` to ensure that jquery.formance.coffee is compiled first when concatenating all fields otherwise the fields wouldn't be able to access $.fn.formance.
65 changes: 11 additions & 54 deletions example/index.html
Original file line number Diff line number Diff line change
@@ -1,57 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="../lib/formance.js"></script>

<style type="text/css" media="screen">
input.invalid {
border: 2px solid red;
}

.validation.failed:after {
color: red;
content: 'Validation failed';
}

.validation.passed:after {
color: green;
content: 'Validation passed';
}
</style>

<script>
jQuery(function($){
$('[data-numeric]').formance('restrictNumeric');
$('.cc-number').formance('formatCreditCardNumber');
$('.cc-exp').formance('formatCreditCardExpiry');
$('.cc-cvc').formance('formatCreditCardCVC');
$('.ontario_outdoors_card_number').formance('formatOntarioOutdoorsCardNumber');

$('form').submit(function(e){
e.preventDefault();
$('input').removeClass('invalid');
$('.validation').removeClass('passed failed');

var cardType = $.formance.cardType($('.cc-number').val());

$('.cc-number').toggleClass('invalid', !$.formance.validateCreditCardNumber($('.cc-number').val()));
$('.cc-exp').toggleClass('invalid', !$.formance.validateCreditCardExpiry($('.cc-exp').formance('cardExpiryVal')));
$('.cc-cvc').toggleClass('invalid', !$.formance.validateCreditCardCVC($('.cc-cvc').val(), cardType));

if ( $('input.invalid').length ) {
$('.validation').addClass('failed');
} else {
$('.validation').addClass('passed');
}
});
});
</script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="../lib/jquery.formance.js"></script>
</head>
<body>

<form novalidate autocomplete="on">
<h2>North America Phone Number formatting '(111) 111 - 111'</h2>
<input type="text" class="phone_number" placeholder="Phone Number">

<h2>Canadian Postal Code 'A1A 1A1'</h2>
<input type="text" class="canadian_postal_code" placeholder="Postal Code">

<h2>Credit Card number formatting</h2>
<input type="text" class="cc-number" pattern="\d*" x-autocompletetype="cc-number" placeholder="Credit Card number" required>

Expand All @@ -62,15 +23,11 @@ <h2>CVC formatting</h2>
<input type="text" class="cc-cvc" pattern="\d*" x-autocompletetype="cc-csc" placeholder="Security code" required autocomplete="off">

<h2>Restrict Numeric</h2>
<input type="text" data-numeric>
<input type="text" placeholder="Only Digits" data-numeric>

<h2>Ontario outdoors card number formatting </h2>
<input type="text" class="ontario_outdoors_card_number" pattern="\d*" placeholder="Outdoors Card Number">

<h2 class="validation"></h2>

<button type="submit">Submit</button>
<input type="text" class="ontario_outdoors_card_number" placeholder="Outdoors Card Number">
</form>

</body>
</html>
</html>
134 changes: 0 additions & 134 deletions lib/fields/credit_card_cvc.js

This file was deleted.

Loading

0 comments on commit d51cceb

Please sign in to comment.