-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepcook.filter.js
221 lines (174 loc) · 4.49 KB
/
prepcook.filter.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* @file
* prepcook.filter
*
* This module contains filter/format functions for formatting tokens
* from prepcook.token.js.
*/
var parseutil = parseutil || require('./includes/prepcook.utils');
const constants = require('./prepcook.config');
const BISTRO_FAILURE = constants.BISTRO_FAILURE;
var prepcookFilter = (function FilterFactory() {
/**
* Format a number as the given currency format.
*
* @see https://www.currency-iso.org/en/home/tables/table-a1.html
* for currency codes.
*
* @param {number} number
* The number to be formatted. May be into or decimal.
* @param {string} type
* The type of currency this should be formatted to.
*
* @return {string}
* The number, formatted to the passed currency [type],
* if supported. At worst, the original number passed.
*/
function filterCurrency (number, type) {
console.log('filterCurrency: ', number, ',', type);
var symbol = '',
dec = 2;
switch (type) {
case 'USD':
case 'EUR':
case 'GBP':
case 'JPY':
case 'INR':
case 'CNY':
break;
default:
console.warn(type + ' is not a recgonized currency format.');
return number;
}
return number.toLocaleString('en-US', {style: 'currency', minimumFractionDigits: dec, currency: type});
}
/**
* Given a date, format as the passed [format].
*
* @param {date} date
* A date to format.
* @param {string} format
* A format which we should apply to our [date].
*
* @return {string}
* Our [date], in the specified format.
*/
function filterDate (date, format) {
try {
if (typeof date !== 'string') {
throw new Error('Date passed to filterDate expected string, ' + typeof date + ' given.');
}
var d = new Date(date);
/**
@TODO
*/
// Allowed formats: 'Y-M-D'...
date = d;
}
catch (error) {
console.warn('Error formatting date. ', err);
return BISTRO_FAILURE;
}
return date;
}
/**
* Filter a list of data by a [filter].
*
* @param {mixed} data
* Some data to filter.
* @param {string} filter
* A filter command to apply to our data.
*
* @return {mixed}
* Our data, with only the elements not removed by the applied [filter].
*/
function filterFilter (data, filter) {
/**
@TODO
*/
return data;
}
/**
* Join all elements of an object into a string, separated by a delimeter.
*
* @param {object|array} data
* The items to join.
* @param {string} delimeter
* The separater between each piece.
*
* @return {string|object}
* The joined object in string format. If object or delimeter were illegal,
* the original data is returned, unmodified.
*/
function filterImplode(data, delimeter) {
if (typeof data !== 'object' || data === null) {
console.warn('Cannot explode non-object.');
return data;
}
else if (typeof delimeter !== 'string') {
console.warn('Cannot Implode object with an invalid delimeter.')
}
return data.join(delimeter);
}
/**
* Filter a string to lowercase
*
* @param {string} data
* A string to convert to lowercase.
*
* @return {string}
* A string, lowercased. If passed type was not a string, it is returned as-is.
*/
function filterLowercase (data) {
if (typeof data !== 'string') {
console.warn('Lowercase filter expects string. ' + typeof data + ' given.');
return data;
}
return data.toLowerCase();
}
/**
* Filter a string to uppercase
*
* @param {string} data
* A string to convert to uppercase.
*
* @return {string}
* A string, uppercased. If passed type was not a string, it is returned as-is.
*/
function filterUppercase (data) {
if (typeof data !== 'string') {
console.warn('Uppercase filter expects string. ' + typeof data + ' given.');
return data;
}
return data.toUpperCase();
}
/**
@TODO
* @param {mixed} object
* Some variables or object to convert to JSON.
*
* @return {JSON}
* Our data, in JSON format.
*/
function filterToJSON (object) {
return JSON.stringify(object);
}
return {
currency: filterCurrency,
date: filterDate,
json: filterToJSON,
implode: filterImplode,
filter: filterFilter,
uppercase: filterUppercase,
lowercase: filterLowercase
};
})();
module.exports = {
currency: prepcookFilter.currency,
date: prepcookFilter.date,
json: prepcookFilter.json,
implode: prepcookFilter.implode,
filter: prepcookFilter.filter,
uppercase: prepcookFilter.uppercase,
lowercase: prepcookFilter.lowercase
};