accountingjs-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 74453cc0cdf3a895649d98f5931949d6b0a19f27
4
+ data.tar.gz: 0c45d2d4cfee35c93baeec959d26f95765859a1b
5
+ SHA512:
6
+ metadata.gz: e44c438a481e52bcfdff336eeb032458a3d0d328fa95eef6ff709e4631fdf00be40b2bc13d321a2a2cf18064888ebb0557c7b2fbc66e316dab3080fee90f5a73
7
+ data.tar.gz: b57447b49faeed5ddf6a451f1114147e3d17dec39de970082f2d056842bc25b6385adde76bd29d94c6d440c85c5e918b74a9c1828c9b3e2c37af802dbb5c80d1
@@ -0,0 +1,39 @@
1
+ # Accounting.js is a tiny JavaScript library for number, money and currency formatting
2
+
3
+ Provides an easy-to-use Rails 3.1 asset for [Accounting.js](http://josscrowcroft.github.com/accounting.js/)
4
+
5
+ # Install
6
+
7
+ Add it to your Rails application's `Gemfile`:
8
+
9
+ ```ruby
10
+ gem 'accountingjs-rails'
11
+ ```
12
+
13
+ Then `bundle install`.
14
+
15
+
16
+ # Usage
17
+
18
+ Add the following to your `app/assets/javascripts/application.js`:
19
+
20
+ //= require accounting
21
+
22
+
23
+ Then you can:
24
+
25
+ // Default usage:
26
+ accounting.formatMoney(12345678); // $12,345,678.00
27
+
28
+ // European formatting (custom symbol and separators), could also use options object as second param:
29
+ accounting.formatMoney(4999.99, "€", 2, ".", ","); // €4.999,99
30
+
31
+ // Negative values are formatted nicely, too:
32
+ accounting.formatMoney(-500000, "£ ", 0); // £ -500,000
33
+
34
+ // Simple `format` string allows control of symbol position [%v = value, %s = symbol]:
35
+ accounting.formatMoney(5318008, { symbol: "GBP", format: "%v %s" }); // 5,318,008.00 GBP
36
+
37
+
38
+ See the full usage details on the [accounting.js](http://josscrowcroft.github.com/accounting.js/) site.
39
+
@@ -0,0 +1,7 @@
1
+ require "jss/version"
2
+
3
+ module Accounting
4
+ module Rails
5
+ require "accounting/engine"
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ module Accounting
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,412 @@
1
+ /*!
2
+ * accounting.js v0.3.2
3
+ * Copyright 2011, Joss Crowcroft
4
+ *
5
+ * Freely distributable under the MIT license.
6
+ * Portions of accounting.js are inspired or borrowed from underscore.js
7
+ *
8
+ * Full details and documentation:
9
+ * http://josscrowcroft.github.com/accounting.js/
10
+ */
11
+
12
+ (function(root, undefined) {
13
+
14
+ /* --- Setup --- */
15
+
16
+ // Create the local library object, to be exported or referenced globally later
17
+ var lib = {};
18
+
19
+ // Current version
20
+ lib.version = '0.3.2';
21
+
22
+
23
+ /* --- Exposed settings --- */
24
+
25
+ // The library's settings configuration object. Contains default parameters for
26
+ // currency and number formatting
27
+ lib.settings = {
28
+ currency: {
29
+ symbol : "$", // default currency symbol is '$'
30
+ format : "%s%v", // controls output: %s = symbol, %v = value (can be object, see docs)
31
+ decimal : ".", // decimal point separator
32
+ thousand : ",", // thousands separator
33
+ precision : 2, // decimal places
34
+ grouping : 3 // digit grouping (not implemented yet)
35
+ },
36
+ number: {
37
+ precision : 0, // default precision on numbers is 0
38
+ grouping : 3, // digit grouping (not implemented yet)
39
+ thousand : ",",
40
+ decimal : "."
41
+ }
42
+ };
43
+
44
+
45
+ /* --- Internal Helper Methods --- */
46
+
47
+ // Store reference to possibly-available ECMAScript 5 methods for later
48
+ var nativeMap = Array.prototype.map,
49
+ nativeIsArray = Array.isArray,
50
+ toString = Object.prototype.toString;
51
+
52
+ /**
53
+ * Tests whether supplied parameter is a string
54
+ * from underscore.js
55
+ */
56
+ function isString(obj) {
57
+ return !!(obj === '' || (obj && obj.charCodeAt && obj.substr));
58
+ }
59
+
60
+ /**
61
+ * Tests whether supplied parameter is a string
62
+ * from underscore.js, delegates to ECMA5's native Array.isArray
63
+ */
64
+ function isArray(obj) {
65
+ return nativeIsArray ? nativeIsArray(obj) : toString.call(obj) === '[object Array]';
66
+ }
67
+
68
+ /**
69
+ * Tests whether supplied parameter is a true object
70
+ */
71
+ function isObject(obj) {
72
+ return obj && toString.call(obj) === '[object Object]';
73
+ }
74
+
75
+ /**
76
+ * Extends an object with a defaults object, similar to underscore's _.defaults
77
+ *
78
+ * Used for abstracting parameter handling from API methods
79
+ */
80
+ function defaults(object, defs) {
81
+ var key;
82
+ object = object || {};
83
+ defs = defs || {};
84
+ // Iterate over object non-prototype properties:
85
+ for (key in defs) {
86
+ if (defs.hasOwnProperty(key)) {
87
+ // Replace values with defaults only if undefined (allow empty/zero values):
88
+ if (object[key] == null) object[key] = defs[key];
89
+ }
90
+ }
91
+ return object;
92
+ }
93
+
94
+ /**
95
+ * Implementation of `Array.map()` for iteration loops
96
+ *
97
+ * Returns a new Array as a result of calling `iterator` on each array value.
98
+ * Defers to native Array.map if available
99
+ */
100
+ function map(obj, iterator, context) {
101
+ var results = [], i, j;
102
+
103
+ if (!obj) return results;
104
+
105
+ // Use native .map method if it exists:
106
+ if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
107
+
108
+ // Fallback for native .map:
109
+ for (i = 0, j = obj.length; i < j; i++ ) {
110
+ results[i] = iterator.call(context, obj[i], i, obj);
111
+ }
112
+ return results;
113
+ }
114
+
115
+ /**
116
+ * Check and normalise the value of precision (must be positive integer)
117
+ */
118
+ function checkPrecision(val, base) {
119
+ val = Math.round(Math.abs(val));
120
+ return isNaN(val)? base : val;
121
+ }
122
+
123
+
124
+ /**
125
+ * Parses a format string or object and returns format obj for use in rendering
126
+ *
127
+ * `format` is either a string with the default (positive) format, or object
128
+ * containing `pos` (required), `neg` and `zero` values (or a function returning
129
+ * either a string or object)
130
+ *
131
+ * Either string or format.pos must contain "%v" (value) to be valid
132
+ */
133
+ function checkCurrencyFormat(format) {
134
+ var defaults = lib.settings.currency.format;
135
+
136
+ // Allow function as format parameter (should return string or object):
137
+ if ( typeof format === "function" ) format = format();
138
+
139
+ // Format can be a string, in which case `value` ("%v") must be present:
140
+ if ( isString( format ) && format.match("%v") ) {
141
+
142
+ // Create and return positive, negative and zero formats:
143
+ return {
144
+ pos : format,
145
+ neg : format.replace("-", "").replace("%v", "-%v"),
146
+ zero : format
147
+ };
148
+
149
+ // If no format, or object is missing valid positive value, use defaults:
150
+ } else if ( !format || !format.pos || !format.pos.match("%v") ) {
151
+
152
+ // If defaults is a string, casts it to an object for faster checking next time:
153
+ return ( !isString( defaults ) ) ? defaults : lib.settings.currency.format = {
154
+ pos : defaults,
155
+ neg : defaults.replace("%v", "-%v"),
156
+ zero : defaults
157
+ };
158
+
159
+ }
160
+ // Otherwise, assume format was fine:
161
+ return format;
162
+ }
163
+
164
+
165
+ /* --- API Methods --- */
166
+
167
+ /**
168
+ * Takes a string/array of strings, removes all formatting/cruft and returns the raw float value
169
+ * alias: accounting.`parse(string)`
170
+ *
171
+ * Decimal must be included in the regular expression to match floats (defaults to
172
+ * accounting.settings.number.decimal), so if the number uses a non-standard decimal
173
+ * separator, provide it as the second argument.
174
+ *
175
+ * Also matches bracketed negatives (eg. "$ (1.99)" => -1.99)
176
+ *
177
+ * Doesn't throw any errors (`NaN`s become 0) but this may change in future
178
+ */
179
+ var unformat = lib.unformat = lib.parse = function(value, decimal) {
180
+ // Recursively unformat arrays:
181
+ if (isArray(value)) {
182
+ return map(value, function(val) {
183
+ return unformat(val, decimal);
184
+ });
185
+ }
186
+
187
+ // Fails silently (need decent errors):
188
+ value = value || 0;
189
+
190
+ // Return the value as-is if it's already a number:
191
+ if (typeof value === "number") return value;
192
+
193
+ // Default decimal point comes from settings, but could be set to eg. "," in opts:
194
+ decimal = decimal || lib.settings.number.decimal;
195
+
196
+ // Build regex to strip out everything except digits, decimal point and minus sign:
197
+ var regex = new RegExp("[^0-9-" + decimal + "]", ["g"]),
198
+ unformatted = parseFloat(
199
+ ("" + value)
200
+ .replace(/\((.*)\)/, "-$1") // replace bracketed values with negatives
201
+ .replace(regex, '') // strip out any cruft
202
+ .replace(decimal, '.') // make sure decimal point is standard
203
+ );
204
+
205
+ // This will fail silently which may cause trouble, let's wait and see:
206
+ return !isNaN(unformatted) ? unformatted : 0;
207
+ };
208
+
209
+
210
+ /**
211
+ * Implementation of toFixed() that treats floats more like decimals
212
+ *
213
+ * Fixes binary rounding issues (eg. (0.615).toFixed(2) === "0.61") that present
214
+ * problems for accounting- and finance-related software.
215
+ */
216
+ var toFixed = lib.toFixed = function(value, precision) {
217
+ precision = checkPrecision(precision, lib.settings.number.precision);
218
+ var power = Math.pow(10, precision);
219
+
220
+ // Multiply up by precision, round accurately, then divide and use native toFixed():
221
+ return (Math.round(lib.unformat(value) * power) / power).toFixed(precision);
222
+ };
223
+
224
+
225
+ /**
226
+ * Format a number, with comma-separated thousands and custom precision/decimal places
227
+ *
228
+ * Localise by overriding the precision and thousand / decimal separators
229
+ * 2nd parameter `precision` can be an object matching `settings.number`
230
+ */
231
+ var formatNumber = lib.formatNumber = function(number, precision, thousand, decimal) {
232
+ // Resursively format arrays:
233
+ if (isArray(number)) {
234
+ return map(number, function(val) {
235
+ return formatNumber(val, precision, thousand, decimal);
236
+ });
237
+ }
238
+
239
+ // Clean up number:
240
+ number = unformat(number);
241
+
242
+ // Build options object from second param (if object) or all params, extending defaults:
243
+ var opts = defaults(
244
+ (isObject(precision) ? precision : {
245
+ precision : precision,
246
+ thousand : thousand,
247
+ decimal : decimal
248
+ }),
249
+ lib.settings.number
250
+ ),
251
+
252
+ // Clean up precision
253
+ usePrecision = checkPrecision(opts.precision),
254
+
255
+ // Do some calc:
256
+ negative = number < 0 ? "-" : "",
257
+ base = parseInt(toFixed(Math.abs(number || 0), usePrecision), 10) + "",
258
+ mod = base.length > 3 ? base.length % 3 : 0;
259
+
260
+ // Format the number:
261
+ return negative + (mod ? base.substr(0, mod) + opts.thousand : "") + base.substr(mod).replace(/(\d{3})(?=\d)/g, "$1" + opts.thousand) + (usePrecision ? opts.decimal + toFixed(Math.abs(number), usePrecision).split('.')[1] : "");
262
+ };
263
+
264
+
265
+ /**
266
+ * Format a number into currency
267
+ *
268
+ * Usage: accounting.formatMoney(number, symbol, precision, thousandsSep, decimalSep, format)
269
+ * defaults: (0, "$", 2, ",", ".", "%s%v")
270
+ *
271
+ * Localise by overriding the symbol, precision, thousand / decimal separators and format
272
+ * Second param can be an object matching `settings.currency` which is the easiest way.
273
+ *
274
+ * To do: tidy up the parameters
275
+ */
276
+ var formatMoney = lib.formatMoney = function(number, symbol, precision, thousand, decimal, format) {
277
+ // Resursively format arrays:
278
+ if (isArray(number)) {
279
+ return map(number, function(val){
280
+ return formatMoney(val, symbol, precision, thousand, decimal, format);
281
+ });
282
+ }
283
+
284
+ // Clean up number:
285
+ number = unformat(number);
286
+
287
+ // Build options object from second param (if object) or all params, extending defaults:
288
+ var opts = defaults(
289
+ (isObject(symbol) ? symbol : {
290
+ symbol : symbol,
291
+ precision : precision,
292
+ thousand : thousand,
293
+ decimal : decimal,
294
+ format : format
295
+ }),
296
+ lib.settings.currency
297
+ ),
298
+
299
+ // Check format (returns object with pos, neg and zero):
300
+ formats = checkCurrencyFormat(opts.format),
301
+
302
+ // Choose which format to use for this value:
303
+ useFormat = number > 0 ? formats.pos : number < 0 ? formats.neg : formats.zero;
304
+
305
+ // Return with currency symbol added:
306
+ return useFormat.replace('%s', opts.symbol).replace('%v', formatNumber(Math.abs(number), checkPrecision(opts.precision), opts.thousand, opts.decimal));
307
+ };
308
+
309
+
310
+ /**
311
+ * Format a list of numbers into an accounting column, padding with whitespace
312
+ * to line up currency symbols, thousand separators and decimals places
313
+ *
314
+ * List should be an array of numbers
315
+ * Second parameter can be an object containing keys that match the params
316
+ *
317
+ * Returns array of accouting-formatted number strings of same length
318
+ *
319
+ * NB: `white-space:pre` CSS rule is required on the list container to prevent
320
+ * browsers from collapsing the whitespace in the output strings.
321
+ */
322
+ lib.formatColumn = function(list, symbol, precision, thousand, decimal, format) {
323
+ if (!list) return [];
324
+
325
+ // Build options object from second param (if object) or all params, extending defaults:
326
+ var opts = defaults(
327
+ (isObject(symbol) ? symbol : {
328
+ symbol : symbol,
329
+ precision : precision,
330
+ thousand : thousand,
331
+ decimal : decimal,
332
+ format : format
333
+ }),
334
+ lib.settings.currency
335
+ ),
336
+
337
+ // Check format (returns object with pos, neg and zero), only need pos for now:
338
+ formats = checkCurrencyFormat(opts.format),
339
+
340
+ // Whether to pad at start of string or after currency symbol:
341
+ padAfterSymbol = formats.pos.indexOf("%s") < formats.pos.indexOf("%v") ? true : false,
342
+
343
+ // Store value for the length of the longest string in the column:
344
+ maxLength = 0,
345
+
346
+ // Format the list according to options, store the length of the longest string:
347
+ formatted = map(list, function(val, i) {
348
+ if (isArray(val)) {
349
+ // Recursively format columns if list is a multi-dimensional array:
350
+ return lib.formatColumn(val, opts);
351
+ } else {
352
+ // Clean up the value
353
+ val = unformat(val);
354
+
355
+ // Choose which format to use for this value (pos, neg or zero):
356
+ var useFormat = val > 0 ? formats.pos : val < 0 ? formats.neg : formats.zero,
357
+
358
+ // Format this value, push into formatted list and save the length:
359
+ fVal = useFormat.replace('%s', opts.symbol).replace('%v', formatNumber(Math.abs(val), checkPrecision(opts.precision), opts.thousand, opts.decimal));
360
+
361
+ if (fVal.length > maxLength) maxLength = fVal.length;
362
+ return fVal;
363
+ }
364
+ });
365
+
366
+ // Pad each number in the list and send back the column of numbers:
367
+ return map(formatted, function(val, i) {
368
+ // Only if this is a string (not a nested array, which would have already been padded):
369
+ if (isString(val) && val.length < maxLength) {
370
+ // Depending on symbol position, pad after symbol or at index 0:
371
+ return padAfterSymbol ? val.replace(opts.symbol, opts.symbol+(new Array(maxLength - val.length + 1).join(" "))) : (new Array(maxLength - val.length + 1).join(" ")) + val;
372
+ }
373
+ return val;
374
+ });
375
+ };
376
+
377
+
378
+ /* --- Module Definition --- */
379
+
380
+ // Export accounting for CommonJS. If being loaded as an AMD module, define it as such.
381
+ // Otherwise, just add `accounting` to the global object
382
+ if (typeof exports !== 'undefined') {
383
+ if (typeof module !== 'undefined' && module.exports) {
384
+ exports = module.exports = lib;
385
+ }
386
+ exports.accounting = lib;
387
+ } else if (typeof define === 'function' && define.amd) {
388
+ // Return the library as an AMD module:
389
+ define([], function() {
390
+ return lib;
391
+ });
392
+ } else {
393
+ // Use accounting.noConflict to restore `accounting` back to its original value.
394
+ // Returns a reference to the library's `accounting` object;
395
+ // e.g. `var numbers = accounting.noConflict();`
396
+ lib.noConflict = (function(oldAccounting) {
397
+ return function() {
398
+ // Reset the value of the root's `accounting` variable:
399
+ root.accounting = oldAccounting;
400
+ // Delete the noConflict method:
401
+ lib.noConflict = undefined;
402
+ // Return reference to the library to re-assign it:
403
+ return lib;
404
+ };
405
+ })(root.accounting);
406
+
407
+ // Declare `fx` on the root (global/window) object:
408
+ root['accounting'] = lib;
409
+ }
410
+
411
+ // Root will be `window` in browser or `global` on the server:
412
+ }(this));
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: accountingjs-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Maksim Berjoza
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email:
15
+ - maksim.berjoza@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/accounting/engine.rb
21
+ - lib/accounting.rb
22
+ - vendor/assets/javascripts/accounting.js
23
+ - README.md
24
+ homepage: https://github.com/torbjon/accountingjs-rails
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.0.2
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: A tiny JavaScript library for number, money and currency formatting with
48
+ Rails asset pipeline
49
+ test_files: []