Skip to content

Commit

Permalink
Replace underscore with a general 'isType' function, which seems suff…
Browse files Browse the repository at this point in the history
…icient
  • Loading branch information
yamadapc committed Dec 21, 2013
1 parent 6676f6d commit 1441e0f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
16 changes: 13 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';
var mongoose = require('mongoose');
var _ = require('underscore');
var util = require('util');

module.exports.loadType = function(mongoose) {
Expand All @@ -19,7 +18,7 @@ function Currency(path, options) {
util.inherits(Currency, mongoose.SchemaTypes.Number);

Currency.prototype.cast = function(val) {
if ( _.isString(val) ) {
if ( isType('String', val) ) {
var currencyAsString = val.toString();
var findDigitsAndDotRegex = /\d*\.\d{1,2}/;
var findCommasAndLettersRegex = /\,+|[a-zA-Z]+/g;
Expand All @@ -32,9 +31,20 @@ Currency.prototype.cast = function(val) {
} else{
return (currency * 100).toFixed(0) * 1;
}
} else if ( _.isNumber(val) ) {
} else if ( isType('Number', val) ) {
return val.toFixed(0) * 1;
} else {
return new Error('Should pass in a number or string');
}
};

/**
* isType(type, obj)
* Supported types: 'Function', 'String', 'Number', 'Date', 'RegExp',
* 'Arguments'
* source: https://github.com/jashkenas/underscore/blob/1.5.2/underscore.js#L996
*/

function isType(type, obj) {
return Object.prototype.toString.call(obj) == '[object ' + type + ']';
}
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
"license": "MIT",
"readmeFilename": "README.md",
"gitHead": "80f9b6fa07591917779b470a3a92c6fe3aadd9fe",
"dependencies": {
"underscore": "1.x"
},
"devDependencies": {
"mocha": "1.9.x",
"should": "*",
Expand Down

0 comments on commit 1441e0f

Please sign in to comment.