-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.md
306 lines (210 loc) · 6.59 KB
/
README.md
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# apex-core-utils
Core utilites for [Apex](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dev_guide.htm).
<a href="https://githubsfdeploy.herokuapp.com/app/githubdeploy/MJ12358/apex-core-utils?ref=main">
<img alt="Deploy to Salesforce"
src="https://raw.githubusercontent.com/afawcett/githubsfdeploy/master/deploy.png">
</a>
## Highlights
- ### BooleanUtil
Utility to convert a boolean to a string and vice versa.
- ### CronBuilder
Used to build cron strings easily via a fluent api.
- ### CsvBuilder
Allows easy creation of csv strings.
- ### DateUtil
Utilities to convert dates to strings and vice versa, check business hours and check holidays.
- ### EmailUtil
Utility to easily send emails.
- ### FileUtil
Utilities to get file icons, sizes, extensions and mimetypes.
- ### HolidayUtil
[Inspired by this awesome code](https://salesforce.stackexchange.com/questions/158547/recurring-holidays-exclude-holidays-between-two-dates)
Used to determine if a date is a holiday.
- ### IntegerIterator
[Inspired by this post](https://salesforce.stackexchange.com/questions/116785/how-to-create-thousands-of-records-using-batch-apex)
Generic iterator used with integers.
- ### ListUtil
Used to convert to and from lists of all types.
- ### MergeFieldUtil
Allows conversion of merge fields within a string (similar to VisualForce).
- ### NumberUtil
Utilities to work with numbers.
- ### StringBuilder
[Inspired by this amazing code](https://github.com/financialforcedev/df12-apex-enterprise-patterns/blob/master/df12/src/classes/StringBuilder.cls)
A string builder similar to Java's.
- ### StringInterator
Generic iterator used with strings.
- ### TypeUtil
Get the type of an object.
- ### UrlUtil
Utilities to work with urls.
- ### XmlReader
Reads xml streams.
## Usage
`BooleanUtil`
```apex
// to a string
String result = BooleanUtil.toString(true);
System.assertEquals('TRUE', result);
// to yes or no
String result = BooleanUtil.toYesNo(false);
System.assertEquals('NO', result);
// from string
Boolean result = BooleanUtil.toBoolean('yes');
System.assertEquals(true, result);
// from integer/double/decimal
Boolean result = BooleanUtil.toBoolean(1);
System.assertEquals(true, result);
```
`CronBuilder`
```apex
CronBuilder builder = new CronBuilder();
builder
.second(45)
.minute(47)
.hour(6)
.month(1);
String result = builder.build();
System.assertEquals('45 47 6 ? 1 ?', result);
```
Or pass it a date or datetime.
```apex
CronBuilder builder = new CronBuilder();
Datetime dt = Datetime.newInstance();
builder.fromDate(dt);
```
`CsvBuilder`
```apex
CsvBuilder builder = new CsvBuilder();
builder
.appendField('Column1', 'value1')
.newRow()
.appendField('Column1', 'value2');
String result = builder.build();
```
`DateUtil`
```apex
Datetime dt = Datetime.now();
// 'yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\''
String iso = DateUtil.toIsoString(dt);
```
`EmailUtil`
```apex
EmailUtil.send('Subject', 'Message', '[email protected]');
String body = 'See you there! On Fri, May 27 Test <[email protected]> wrote: This is a previous message.';
String result = EmailUtil.getVisibleText(body);
System.assertEquals('See you there!', result);
```
`FileUtil`
```apex
String size = FileUtil.sizeToString(0);
System.assertEquals('0 Bytes', size);
String size = FileUtil.sizeToString(2048);
System.assertEquals('2 KB', size);
```
`HolidayUtil`
```apex
Date newYears = Date.newinstance(2020, 1, 1);
Boolean result = HolidayUtil.isHoliday(newYears);
// assuming Jan 1 is a holiday in your org
System.assertEquals(true, result);
// retrieve all holidays between two dates
Map<Date, Holiday> result = HolidayUtil.getBetweenDates(Date.today(), Date.today().addYears(1));
```
`IntegerIterator`
```apex
IntegerIterator it = new IntegerIterator(500);
while (it.hasNext()) {
Integer next = it.next();
}
```
`ListUtil`
```apex
List<String> myList = new List<String>{'one', 'two', 'three'};
List<List<String>> result = ListUtil.chunk(myList, 2);
System.assertEquals(2, result.size());
List<String> result = ListUtil.reverse(myList);
System.assertEquals(new List<String>{'three', 'two', 'one'}, result);
```
`MergeFieldUtil`
```apex
Account acc = new Account();
acc.Name = 'Test Company';
String template = 'Hi {!Name}. Im {!$User.Name}';
String result = MergeFieldUtil.resolve(acc, template);
System.assertEquals('Hi Test Company. Im MJ12358);
```
`NumberUtil`
```apex
String result = NumberUtil.getOrdinal(223);
System.assertEquals('223rd', result);
Integer result = NumberUtil.hexToInt('abc');
System.assertEquals(2748, result);
String result = NumberUtil.intToHex(2748);
System.assertEquals('ABC', result);
```
`StringBuilder`
```apex
StringBuilder builder = new StringBuilder();
builder
.separator(' ')
.add('a')
.add(new List<String>{'b', 'c'});
String result = builder.toString();
System.assertEquals('a b c', result);
// you can also pass it a field set or a list of SObjectField
StringBuilder.FieldListBuilder builder = new StringBuilder.FieldListBuilder(Account.SObjectType);
String result = builder.toString();
System.assertEquals('All,Your,Account,Fields', result);
```
`StringIterator`
```apex
String value = 'Test1,Test2,Test3';
StringIterator it = new StringIterator(value);
while (it.hasNext()) {
String next = it.next();
}
```
`StringUtil`
```apex
String word1 = 'Account';
String word2 = 'Contact';
String result1 = StringUtil.getIndefiniteArticle(word1);
String result2 = StringUtil.getIndefiniteArticle(word2);
System.assertEquals('an', result1);
System.assertEquals('a', result2);
// great for when a third party api utilizes an apex reserved word
String json = '{"abstract": "value", "decimal": "value"};
String result = StringUtil.normalizeKeys(json);
System.assertEquals('{"abstractx": "value", "decimalx": "value"});
```
`TypeUtil`
```apex
System.Type t = TypeUtil.get('testing');
System.assertEquals(String.class, t);
System.Type t = TypeUtil.get(256);
System.assertEquals(Integer.class, t);
String s = TypeUtil.getAsString(256);
System.assertEquals('Integer', s);
```
## Tests
Current test results:
| Class | Percent | Lines |
| ----- | ------- | ----- |
| BooleanUtil | 100% | 31/31 |
| CronBuilder | 85% | 166/195 |
| CsvBuilder | 97% | 45/46 |
| DateUtil | 91% | 234/257 |
| EmailUtil | 62% | 44/70 |
| FileUtil | 84% | 146/173 |
| HolidayUtil | 71% | 119/167 |
| IntegerIterator | 75% | 18/24 |
| ListUtil | 92% | 445/482 |
| MergeFieldUtil | 80% | 54/67 |
| NumberUtil | 95% | 83/87 |
| StringBuilder | 81% | 49/60 |
| StringIterator | 100% | 18/18 |
| StringUtil | 94% | 143/151 |
| TypeUtil | 93% | 44/47 |
| UrlUtil | 63% | 41/65 |
| XmlReader | 100% | 31/31 |