Skip to content

Commit dd7c526

Browse files
committed
Merge pull request DefinitelyTyped#1521 from gerich-home/master
accounting.js type definitions
2 parents 3934651 + 6294f93 commit dd7c526

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Other means to get the definitions
2626

2727
List of Definitions
2828
-------------------
29+
* [accounting.js](http://josscrowcroft.github.io/accounting.js/) (by [Sergey Gerasimov](https://github.com/gerich-home))
2930
* [Ace Cloud9 Editor](http://ace.ajax.org/) (by [Diullei Gomes](https://github.com/Diullei))
3031
* [Add To Home Screen] (http://cubiq.org/add-to-home-screen) (by [James Wilkins] (http://www.codeplex.com/site/users/view/jamesnw))
3132
* [AmCharts](http://www.amcharts.com/) (by [Covobonomo](https://github.com/covobonomo/))

accounting/accounting-tests.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/// <reference path="accounting.d.ts"/>
2+
3+
// formatMoney
4+
5+
// Default usage:
6+
accounting.formatMoney(12345678); // $12,345,678.00
7+
8+
// European formatting (custom symbol and separators), could also use options object as second param:
9+
accounting.formatMoney(4999.99, "€", 2, ".", ","); // €4.999,99
10+
11+
// Negative values are formatted nicely, too:
12+
accounting.formatMoney(-500000, "£ ", 0); // £ -500,000
13+
14+
// Simple `format` string allows control of symbol position [%v = value, %s = symbol]:
15+
accounting.formatMoney(5318008, { symbol: "GBP", format: "%v %s" }); // 5,318,008.00 GBP
16+
17+
// Example usage with options object:
18+
accounting.formatMoney(5318008, {
19+
symbol: "GBP",
20+
precision: 0,
21+
thousand: "·",
22+
format: {
23+
pos: "%s %v",
24+
neg: "%s (%v)",
25+
zero: "%s --"
26+
}
27+
});
28+
29+
// Will recursively format an array of values:
30+
accounting.formatMoney([123, 456, [78, 9]], "$", 0); // ["$123", "$456", ["$78", "$9"]]
31+
32+
33+
34+
// formatColumn
35+
36+
// Format list of numbers for display:
37+
accounting.formatColumn([123.5, 3456.49, 777888.99, 12345678, -5432], "$ ");
38+
39+
// Example usage (NB. use a space after the symbol to add arbitrary padding to all values):
40+
accounting.formatColumn([123, 12345], "$ ", 0); // ["$ 123", "$ 12,345"]
41+
42+
// List of numbers can be a multi-dimensional array (formatColumn is applied recursively):
43+
accounting.formatColumn([[1, 100], [900, 9]]); // [["$ 1.00", "$100.00"], ["$900.00", "$ 9.00"]]
44+
45+
46+
47+
// formatNumber
48+
49+
// Example usage:
50+
accounting.formatNumber(5318008); // 5,318,008
51+
accounting.formatNumber(9876543.21, 3, " "); // 9 876 543.210
52+
accounting.formatNumber(4999.99, 2, ".", ","); // 4.999,99
53+
54+
// Example usage with options object:
55+
accounting.formatNumber(5318008, {
56+
precision: 3,
57+
thousand: " "
58+
});
59+
60+
// Will recursively format an array of values:
61+
accounting.formatNumber([123456, [7890, 123]]); // ["123,456", ["7,890", "123"]]
62+
63+
64+
65+
// toFixed
66+
67+
(0.615).toFixed(2); // "0.61"
68+
accounting.toFixed(0.615, 2); // "0.62"
69+
70+
71+
72+
73+
// unformat
74+
75+
// Example usage:
76+
accounting.unformat("£ 12,345,678.90 GBP"); // 12345678.9
77+
accounting.unformat("GBP £ 12,345,678.90"); // 12345678.9
78+
79+
// If a non-standard decimal separator was used (eg. a comma) unformat() will need it in order to work out
80+
// which part of the number is a decimal/float:
81+
accounting.unformat("€ 1.000.000,00", ","); // 1000000
82+
83+
// Settings object that controls default parameters for library methods:
84+
accounting.settings = {
85+
currency: {
86+
symbol: "$", // default currency symbol is '$'
87+
format: "%s%v", // controls output: %s = symbol, %v = value/number (can be object: see below)
88+
decimal: ".", // decimal point separator
89+
thousand: ",", // thousands separator
90+
precision: 2 // decimal places
91+
},
92+
number: {
93+
precision: 0, // default precision on numbers is 0
94+
thousand: ",",
95+
decimal: "."
96+
}
97+
};
98+
99+
// These can be changed externally to edit the library's defaults:
100+
accounting.settings.currency.format = "%s %v";
101+
102+
// Format can be an object, with `pos`, `neg` and `zero`:
103+
accounting.settings.currency.format = {
104+
pos: "%s %v", // for positive values, eg. "$ 1.00" (required)
105+
neg: "%s (%v)", // for negative values, eg. "$ (1.00)" [optional]
106+
zero: "%s -- " // for zero values, eg. "$ --" [optional]
107+
};

accounting/accounting.d.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Type definitions for accounting.js 0.3.2
2+
// Project: http://josscrowcroft.github.io/accounting.js/
3+
// Definitions by: Sergey Gerasimov <https://github.com/gerich-home/>
4+
// Definitions: https://github.com/borisyankov/DefinitelyTyped
5+
6+
interface IAccountingCurrencyFormat {
7+
pos: string; // for positive values, eg. "$ 1.00"
8+
neg?: string; // for negative values, eg. "$ (1.00)"
9+
zero?: string; // for zero values, eg. "$ --"
10+
}
11+
12+
interface IAccountingCurrencySettings<TFormat> {
13+
symbol?: string; // default currency symbol is '$'
14+
format?: TFormat; // controls output: %s = symbol, %v = value/number
15+
decimal?: string; // decimal point separator
16+
thousand?: string; // thousands separator
17+
precision?: number // decimal places
18+
}
19+
20+
interface IAccountingNumberSettings {
21+
precision?: number; // default precision on numbers is 0
22+
thousand?: string;
23+
decimal?: string;
24+
}
25+
26+
interface IAccountingSettings {
27+
currency: IAccountingCurrencySettings<any>; // IAccountingCurrencySettings<string> or IAccountingCurrencySettings<IAccountingCurrencyFormat>
28+
number: IAccountingNumberSettings;
29+
}
30+
31+
interface IAccountingStatic {
32+
// format any number into currency
33+
formatMoney(number: number, symbol?: string, precision?: number, thousand?: string, decimal?: string, format?: string): string;
34+
formatMoney(number: number, options: IAccountingCurrencySettings<string>): string;
35+
formatMoney(number: number, options: IAccountingCurrencySettings<IAccountingCurrencyFormat>): string;
36+
37+
formatMoney(numbers: number[], symbol?: string, precision?: number, thousand?: string, decimal?: string, format?: string): string[];
38+
formatMoney(numbers: number[], options: IAccountingCurrencySettings<string>): string[];
39+
formatMoney(numbers: number[], options: IAccountingCurrencySettings<IAccountingCurrencyFormat>): string[];
40+
41+
// generic case (any array of numbers)
42+
formatMoney(numbers: any[], symbol?: string, precision?: number, thousand?: string, decimal?: string, format?: string): any[];
43+
formatMoney(numbers: any[], options: IAccountingCurrencySettings<string>): any[];
44+
formatMoney(numbers: any[], options: IAccountingCurrencySettings<IAccountingCurrencyFormat>): any[];
45+
46+
// format a list of values for column-display
47+
formatColumn(numbers: number[], symbol?: string, precision?: number, thousand?: string, decimal?: string, format?: string): string[];
48+
formatColumn(numbers: number[], options: IAccountingCurrencySettings<string>): string[];
49+
formatColumn(numbers: number[], options: IAccountingCurrencySettings<IAccountingCurrencyFormat>): string[];
50+
51+
formatColumn(numbers: number[][], symbol?: string, precision?: number, thousand?: string, decimal?: string, format?: string): string[][];
52+
formatColumn(numbers: number[][], options: IAccountingCurrencySettings<string>): string[][];
53+
formatColumn(numbers: number[][], options: IAccountingCurrencySettings<IAccountingCurrencyFormat>): string[][];
54+
55+
// format a number with custom precision and localisation
56+
formatNumber(number: number, precision?: number, thousand?: string, decimal?: string): string;
57+
formatNumber(number: number, options: IAccountingNumberSettings): string;
58+
59+
formatNumber(number: number[], precision?: number, thousand?: string, decimal?: string): string[];
60+
formatNumber(number: number[], options: IAccountingNumberSettings): string[];
61+
62+
formatNumber(number: any[], precision?: number, thousand?: string, decimal?: string): any[];
63+
formatNumber(number: any[], options: IAccountingNumberSettings): any[];
64+
65+
// better rounding for floating point numbers
66+
toFixed(number: number, precision?: number): string;
67+
68+
// get a value from any formatted number/currency string
69+
unformat(string: string, decimal?: string): number;
70+
71+
// settings object that controls default parameters for library methods
72+
settings: IAccountingSettings;
73+
}
74+
75+
declare var accounting: IAccountingStatic;
76+
77+
declare module "accounting" {
78+
export = accounting;
79+
}

0 commit comments

Comments
 (0)