Ember CLI Addon for Accounting.js
Ember-cli-accounting is an Ember CLI addon that integrates the `accounting.js` library into Ember applications, providing robust number and currency formatting capabilities. It ports `accounting.js` (specifically version 0.4.1 as of addon version 2.1.0) to ES6 modules, allowing for selective imports to optimize bundle size. The addon ships with both JavaScript functions and convenient Handlebars helpers (`format-number`, `format-money`) for use directly in templates. It also supports configuring default formatting options via Ember initializers. The addon's versioning does not match the underlying `accounting.js` library, but the maintainer aims to keep it updated with the original library's bug fixes and features. The current stable version is 2.1.1, with recent updates addressing Ember deprecations. It is distinct from directly including `accounting.js` by offering deep Ember integration and ES module support.
Common errors
-
DEPRECATION: Ember.assign is deprecated. Please use `Object.assign` instead.
cause Older versions of `ember-cli-accounting` used `Ember.assign` internally.fixUpdate `ember-cli-accounting` to version 2.1.1 or higher: `ember update ember-cli-accounting` or `npm install ember-cli-accounting@latest`. -
Could not find module `accounting/format-money`
cause Typo in the import path or attempting to `require` an ES module.fixEnsure the import path is exact: `import formatMoney from 'accounting/format-money';`. Confirm your Ember CLI project is configured for ES modules.
Warnings
- breaking Ember compatibility requires specific addon versions. Version 1.0.0+ is for Ember 1.13 and 2.0+, while 0.1.x is for Ember 1.10+ (including 1.13) but *not* Ember 2.0. Version 0.0.4 is for older than Ember 1.10.
- deprecated Usage of `Ember.assign` was deprecated in Ember and has been removed in `ember-cli-accounting` v2.1.1. Using older versions with newer Ember CLI might surface deprecation warnings related to this.
- gotcha The addon's versioning (`ember-cli-accounting` vX.Y.Z) does not directly correspond to the version of the `accounting.js` library it bundles. This can be confusing when trying to track features or bug fixes from the upstream library.
Install
-
npm install ember-cli-accounting -
yarn add ember-cli-accounting -
pnpm add ember-cli-accounting
Imports
- formatMoney
import { formatMoney } from 'accounting'; // Incorrect path const formatMoney = require('accounting/format-money'); // CJS is not the primary usageimport formatMoney from 'accounting/format-money';
- accounting
import * as accounting from 'accounting'; // Not a namespace import const accounting = require('accounting'); // CJS is not the primary usageimport accounting from 'accounting';
- currency, number
import { settings } from 'accounting'; // Incorrect path import { currency } from 'accounting'; // Incorrect pathimport { currency, number } from 'accounting/settings';
Quickstart
import formatMoney from 'accounting/format-money';
import formatNumber from 'accounting/format-number';
// app/initializers/accounting-defaults.js
import { currency, number } from 'accounting/settings';
export default {
name: 'accounting-defaults',
initialize() {
currency.symbol = '€';
currency.format = '%v %s'; // e.g., '123.45 €'
number.decimal = ',';
number.thousand = '.';
number.precision = 2;
}
};
// app/components/my-component.js
import Component from '@ember/component';
import { computed } from '@ember/object';
export default Component.extend({
price: 12345.67,
quantity: 5,
formattedPrice: computed('price', function() {
return formatMoney(this.price, { symbol: '$', precision: 2 }); // Uses local options
}),
// In a Handlebars template (app/templates/components/my-component.hbs):
// <h1>Product Details</h1>
// <p>Price: {{format-money price}}</p> <!-- Uses initializer defaults: '12.345,67 €' -->
// <p>Quantity: {{format-number quantity}}</p> <!-- Uses initializer defaults: '5' -->
// <p>Discounted Price: {{format-money 99.99 symbol='USD' format='%s %v'}}</p> <!-- Overrides for specific use -->
});