Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the Mongoose dependencies #12

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 19 additions & 29 deletions index.js
@@ -1,50 +1,40 @@
'use strict';
var mongoose = require('mongoose');
var util = require('util');
const mongoose = require('mongoose');
const util = require('util');

module.exports.loadType = function(mongoose) {
mongoose.Types.Currency = mongoose.SchemaTypes.Currency = Currency;
return Currency;
};
const REGEX_FIND_DIGITS_AND_DOT = /\d*\.\d{1,2}/;
const REGEX_FIND_COMMAS_AND_LETTERS = /\,+|[a-zA-Z]+/g;
const REGEX_FIND_NEGATIVE = /^-/;

function Currency(path, options) {
mongoose.SchemaTypes.Number.call(this, path, options);
mongoose
.SchemaTypes
.Number
.call(this, path, options);
}

/*!
* inherits
*/

util.inherits(Currency, mongoose.SchemaTypes.Number);

Currency.prototype.cast = function(val) {
if ( isType('String', val) ) {
var currencyAsString = val.toString();
var findDigitsAndDotRegex = /\d*\.\d{1,2}/;
var findCommasAndLettersRegex = /\,+|[a-zA-Z]+/g;
var findNegativeRegex = /^-/;
var currency;
currencyAsString = currencyAsString.replace(findCommasAndLettersRegex, "");
currency = findDigitsAndDotRegex.exec(currencyAsString + ".0")[0]; // Adds .0 so it works with whole numbers
if ( findNegativeRegex.test(currencyAsString) ) {
if (typeof val === 'string') {
let currencyAsString = val.toString();
currencyAsString = currencyAsString.replace(REGEX_FIND_COMMAS_AND_LETTERS, "");
const currency = REGEX_FIND_DIGITS_AND_DOT.exec(currencyAsString + ".0")[0]; // Adds .0 so it works with whole numbers
if (REGEX_FIND_NEGATIVE.test(currencyAsString)) {
return (currency * -100).toFixed(0) * 1;
} else{
} else {
return (currency * 100).toFixed(0) * 1;
}
} else if ( isType('Number', val) ) {
} else if (typeof val === 'number') {
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 + ']';
module.exports.loadType = function(mongoose) {
mongoose.Types.Currency = mongoose.SchemaTypes.Currency = Currency;
return Currency;
}