all files / keystone/fields/types/money/ MoneyType.js

86.67% Statements 26/30
85% Branches 17/20
100% Functions 2/2
86.67% Lines 26/30
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55                                                          
var FieldType = require('../Type');
var NumberType = require('../number/NumberType');
var numeral = require('numeral');
var util = require('util');
 
/**
 * Money FieldType Constructor
 * @extends Field
 * @api public
 */
function money (list, path, options) {
	this.currency = options.currency;
	this._nativeType = Number;
	this._underscoreMethods = ['format'];
	this._properties = ['currency'];
	this._fixedSize = 'small';
	this._formatString = (options.format === false) ? false : (options.format || '$0,0.00');
	if (this._formatString && typeof this._formatString !== 'string') {
		throw new Error('FieldType.Money: options.format must be a string.');
	}
	money.super_.call(this, list, path, options);
}
util.inherits(money, FieldType);
 
 
money.prototype.validateInput = NumberType.prototype.validateInput;
money.prototype.validateRequiredInput = NumberType.prototype.validateRequiredInput;
 
/* Inherit from NumberType prototype */
money.prototype.updateItem = NumberType.prototype.updateItem;
money.prototype.inputIsValid = NumberType.prototype.inputIsValid;
money.prototype.addFilterToQuery = NumberType.prototype.addFilterToQuery;
 
/**
 * Formats the field value
 */
money.prototype.format = function (item, format) {
	Iif (this.currency) {
		try {
			numeral.language(this.currency, require('numeral/languages/' + this.currency));
			numeral.language(this.currency);
		} catch (err) {
			throw new Error('FieldType.Money: options.currency failed to load.');
		}
	}
	if (format || this._formatString) {
		return (typeof item.get(this.path) === 'number') ? numeral(item.get(this.path)).format(format || this._formatString) : '';
	} else {
		return item.get(this.path) || '';
	}
};
 
/* Export Field Type */
module.exports = money;