forked from rickaramirez/FileMaker-DictionaryCustomFunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomFunctionsSuite.xml
480 lines (380 loc) · 17.6 KB
/
CustomFunctionsSuite.xml
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
<fmxmlsnippet type="FMObjectList"><CustomFunction id="4" functionArity="3" visible="True" parameters="dict;key;value" name="DictAfterKeyValue"><Calculation><![CDATA[// DictAfterKeyValue ( dict; key; value )
// version 1.0, Daniel A. Shockley
// Returns part of the specified dictionary that occurs after (and including) the specified key/value pair. If not found, returns empty string.
Let( [
kvPair = DictItem( key; value)
; kvPos = Position( dict; kvPair; 1; 1 ) // will be 0 if not within dict
];
If( kvPos
; Right( dict; Length( dict ) - kvPos + 1 )
; ""
)
)]]></Calculation></CustomFunction><CustomFunction id="5" functionArity="2" visible="True" parameters="dict;name" name="DictContains"><Calculation><![CDATA[// DictContains( dict; name )
// version 1.1, Daniel A. Shockley
// Simple test for whether a dictionary includes a specified key.
/*
VERSION HISTORY:
1.1 - Use DictEscape instead of hard-coded Substitute within function.
*/
Let(
pattern = "<:" & DictEscape( name ) & ":=";
Position( dict; pattern; 1; 1 ) > 0
)]]></Calculation></CustomFunction><CustomFunction id="6" functionArity="2" visible="True" parameters="dict;name" name="DictCount"><Calculation><![CDATA[// DictCount ( dict; name )
// version 1.1, Daniel A. Shockley
// Counts number of name key-value pairs. Useful if you have a "non-standard" dictionary (a standard associative array has only ONE occurrence of a given key-value pair).
/*
VERSION HISTORY:
1.1 - Use DictEscape instead of hard-coded Substitute within function.
*/
Let(
pattern = "<:" & DictEscape( name ) & ":=";
PatternCount( dict; pattern )
)]]></Calculation></CustomFunction><CustomFunction id="7" functionArity="1" visible="True" parameters="input" name="DictEscape"><Calculation><![CDATA[// DictEscape ( input )
// version 1.0, SixFriedRice.com
// Escapes a string (key or value) so it can be used in a SixFriedRice.com-style dictionary.
Substitute( input; ["="; "/="]; [":"; "/:"] ; [">"; "/>"]; ["<" ; "/<"] )]]></Calculation></CustomFunction><CustomFunction id="8" functionArity="1" visible="True" parameters="dict" name="DictFirst"><Calculation><![CDATA[// DictFirst( dict )
// version 1.2, Daniel A. Shockley
// Gets the NAME of the first key-value pair from a Dictionary object.
/*
VERSION HISTORY:
1.2 - Reformatted to match standards.
1.1 - Properly unescapes the name of the item, since returning the escaped name is wrong.
*/
Let( [
startValue = "<:"
; endValue = ":="
; positionOfStartValue = Position( dict ; startValue ; 1 ; 1 )
; endOfStartValue = If( positionOfStartValue > 0 ; positionOfStartValue + Length( startvalue ); -1 )
; beginningOfEndValue = Position( dict; endValue; endOfStartValue; 1 )
; found = If( beginningOfEndValue > -1 and endOfStartValue > -1 ; True; False )
; lengthFoundValue = beginningOfEndValue - endOfStartValue
; foundValue = Middle( dict; endOfStartValue ; lengthFoundValue )
];
If( found ; DictUnescape( foundValue ) ; "" )
)]]></Calculation></CustomFunction><CustomFunction id="9" functionArity="2" visible="True" parameters="dict;name" name="DictGet"><Calculation><![CDATA[// DictGet ( dict; name )
// version 1.1, SixFriedRice.com
// Gets the specified key name's value from a SixFriedRice.com-style dictionary.
/*
VERSION HISTORY:
1.1 - Reformatted calc to fit standards. Still uses hard-coded escape/unescape so it can stand alone.
*/
Let( [
match ="<:" & Substitute( name ; ["="; "/="]; [":"; "/:"] ; [">"; "/>"]; ["<" ; "/<"] ) & ":="
; pstart = Position( dict; match; 1; 1 )
; start = pstart + Length( match )
; end = Position( dict ; ":>"; start; 1 )
; len = If( end = 0; 999999999; end - start )
];
If ( pstart = 0
; ""
; Substitute( Middle( dict; start; len); ["/:"; ":"]; ["/="; "="] ; ["/>"; ">"] ; ["/<" ; "<" ] )
)
)]]></Calculation></CustomFunction><CustomFunction id="23" functionArity="3" visible="True" parameters="dict;pairName;pairIndex" name="DictGetByKeyWithIndex"><Calculation><![CDATA[// DictGetByKeyWithIndex ( dict; pairName; pairIndex )
// version 1.1, Daniel A. Shockley
// For a given SixFriedRice.com-style dictionary that has multiple name-value pairs with the same name, return the value of the pair found at the specified index.
/*
VERSION HISTORY:
1.1 - Reformatted calc to fit standards.
*/
Let( [
match="<:" & DictEscape( pairName ) & ":="
; pstart = Position( dict; match; 1; pairIndex )
; start = pstart + Length( match )
; end = Position( dict ; ":>"; start; 1)
; len = If( end = 0; 999999999; end - start )
];
If ( pstart = 0
; ""
; DictUnescape( Middle( dict; start; len ) )
)
)]]></Calculation></CustomFunction><CustomFunction id="10" functionArity="2" visible="True" parameters="name;value" name="DictItem"><Calculation><![CDATA[// DictItem ( name; value )
// version 1.0, SixFriedRice.com
// Creates a name/value (aka key/value) pair for a SixFriedRice.com-style dictionary.
"<:"
& DictEscape( name )
& ":="
& DictEscape( value )
& ":>"]]></Calculation></CustomFunction><CustomFunction id="11" functionArity="1" visible="True" parameters="dict" name="DictListKeys"><Calculation><![CDATA[// DictListKeys( dict )
// version 1.1, Daniel A. Shockley
// Lists all of the keys present in a dictionary object (as used by SixFriedRice.com).
/*
VERSION HISTORY:
1.1 - Reformatted calc to fit standards.
*/
Let( [
firstKeyName = DictFirst( dict )
// Need to get next key so that we know whether to put in the line return (if no next, don't put in the separator)
; nextKeyName = If( Length( firstKeyName ) > 0; DictFirst( DictRemove( dict; firstKeyName ) ); "" )
];
If( Length( firstKeyName ) > 0
; firstKeyName & If( Length( nextKeyName ) = 0; ""; "¶" & DictListKeys( DictRemove( dict; firstKeyName ) ) )
; ""
)
)]]></Calculation></CustomFunction><CustomFunction id="22" functionArity="2" visible="True" parameters="dict;nodePath" name="DictNode"><Calculation><![CDATA[// DictNode( dict; nodePath )
// version 1.0, Daniel A. Shockley
/* Obtains the value of a specific node from a SixFriedRice.com-style Dictionary using the specified path. Somewhat like the XPath function for XML data.
SYNTAX for the nodePath is:
-----------------------
KeyA/KeyB[2]/…/KeyX
-----------------------
Note that there can be an index in square brackets if there are multiple keys with same name at a level ( KeyB[2] in the example above ).
Note also that while this syntax looks like an XML XPath, it behaves differently. It selects only the first node, not all nodes with a given name, when no index is specified.
VERSION HISTORY:
1.0 - initial version.
EXAMPLE USE:
If the some dictionary is set to the following calc:
-----------------------
#( "FirstName"; "Bob" )
& #( "LastName"; "Smith" )
& #( "PhoneList"
; #( "Phone"
; #( "Label"; "Home" ) & #( "Value"; "212-123-4567" )
)
& #( "Phone"
; #( "Label"; "Cell" ) & #( "Value"; "212-999-9876" )
)
)
-----------------------
Searching for the nodePath "PhoneList/Phone[2]/Label" will return: "Cell"
-----------------------
*/
Let( [
nodeDelim = "/"
; delimPos = Position( nodePath; nodeDelim; 1; 1 )
; firstNode = If( delimPos = 0
; nodePath
; Left( nodePath; Position( nodePath; nodeDelim; 1; 1 ) - 1 )
)
; bracketOpenPos = Position( firstNode; "["; 1; PatternCount( firstNode; "[" ) )
; bracketClosePos = Position( firstNode; "]"; bracketOpenPos; 1 )
; bracketVal =
Case(
bracketOpenPos = 0 or bracketClosePos = 0; "" // no index specified, so use first
;
Middle( firstNode; bracketOpenPos+1; bracketClosePos - bracketOpenPos )
)
; bracketValAsNum = GetAsNumber( bracketVal )
; nodeIndex = If( bracketValAsNum > 0 and bracketValAsNum = Truncate( bracketValAsNum; 0 )
; bracketValAsNum
; ""
)
; firstNodeName = If( IsEmpty( nodeIndex ); firstNode; Left( firstNode; bracketOpenPos - 1 ) )
; nodeValue = If( IsEmpty( nodeIndex ); DictGet( dict; firstNodeName ); DictGetByKeyWithIndex( dict; firstNodeName; nodeIndex ) )
; nodeFinalResult = If( PatternCount( nodePath; nodeDelim ) = 0
; nodeValue // final node of the path, so just return its value
; DictNode( nodeValue; Right( nodePath; Length( nodePath ) - delimPos + 1 - Length( nodeDelim ) ) )
)
];
nodeFinalResult
/* DEBUGGING CODE:
If( firstNodeName = "PhoneList"
; Let([
$$DEBUG_34546576867_1 = "part 1"
; $$DEBUG_34546576867_2 = "dict = " & dict
];
nodeFinalResult
)
; nodeFinalResult
)
*/
)]]></Calculation></CustomFunction><CustomFunction id="12" functionArity="2" visible="True" parameters="dict;name" name="DictRemove"><Calculation><![CDATA[// DictRemove( dict; name )
// version 1.1, SixFriedRice.com
// Removes a name/value (aka key/value) pair from a SixFriedRice.com-style dictionary.
/*
VERSION HISTORY:
1.1 - Reformatted calc to fit standards.
*/
Let([
pattern = "<:" & DictEscape( name ) & ":="
; entry_start = Position( dict ; pattern ; 1 ; 1)
; entry_end = Position( dict ; ":>" ; entry_start + 1; 1)
; dict_beginning = If( entry_start > 0 ; Left ( dict ; entry_start - 1 ) )
; rest_of_dict = Middle( dict ; entry_end + 2 ; Length( dict ) )
; new_dict = dict_beginning & rest_of_dict
];
If( entry_start > 0
; new_dict
; dict
)
)]]></Calculation></CustomFunction><CustomFunction id="13" functionArity="3" visible="True" parameters="dict;name;newValue" name="DictReplace"><Calculation><![CDATA[// DictReplace( dict; name; newValue )
// version 1.1, SixFriedRice.com
// Replaces (the first occurrence of) a key-value pair in a SixFriedRice.com-style Dictionary with a new specified value.
/*
VERSION HISTORY:
1.1 - Changed the order - new key/value pair is before the remaining original dictionary. Reason: If the dictionary contains multiple pairs named with same key, then the one that is "replaced" will be the first one, but the old method would then put the new one at the end. Putting it FIRST in the result means it will appear at the same location as the one it is replacing if the values were extracted for all of the specified (multiple) pairs. Nothing should have relied on the previous behavior, but this new behavior is more consistent with what the function's name implies.
*/
DictItem( name; newValue ) & DictRemove( dict; name )]]></Calculation></CustomFunction><CustomFunction id="21" functionArity="2" visible="True" parameters="dict;subLevel" name="DictToCalcString"><Calculation><![CDATA[// DictToCalcString( dict; subLevel )
// version 1.1, Daniel A. Shockley
/* Displays a SixFriedRice.com-style Dictionary as the calculation that would be needed to create it.
VERSION HISTORY:
1.1 - fixed a bug where there would be a trailing comma.
1.0 - initial version.
EXAMPLE USE:
If the variable $someDictionary is set to the following calc:
-----------------------
#( "FirstName"; "Bob" )
& #( "LastName"; "Smith" )
& #( "PhoneList"
; #( "Phone"
; #( "Label"; "Home" ) & #( "Value"; "212-123-4567" )
)
& #( "Phone"
; #( "Label"; "Cell" ) & #( "Value"; "212-999-9876" )
)
)
-----------------------
and thus $someDictionary looks like this in the debugger:
-----------------------
<:FirstName:=Bob:><:LastName:=Smith:><:PhoneList:=/</:Phone/:/=//<//:Label//://=Home//://>//<//:Value//://=212-123-4567//://>/:/>/</:Phone/:/=//<//:Label//://=Cell//://>//<//:Value//://=212-999-9876//://>/:/>:>
-----------------------
Using DictToCalcString( $someDictionary; "" ) will give back a string that looks just like the calculation above:
-----------------------
*/
Let( [
indentPattern = " " // character(s) to use as prefix for each level of indentation
; subLevel = If( IsEmpty( subLevel ); 0; subLevel ) // identifies what is current level of indent
; firstKey = DictFirst ( dict )
];
If( IsEmpty( firstKey )
; ""
; Let( [
rawData = DictGet( dict; firstKey )
// if the value of this key is ITSELF a dictionary, need to handle that and recurse:
; valueAsDict = If( IsEmpty( DictFirst( rawData ) ) ; ""; "# ( " & Substitute( DictToCalcString( rawData; subLevel + 1 ); "¶"; "¶" & indentPattern ) )
; rest = DictRemove( dict ; firstKey )
];
If( subLevel = 0; "#( " )
& If( IsEmpty( valueAsDict )
; Quote( firstKey ) & "; " & Quote( rawData ) & " ) "
; Quote( firstKey ) & "; ¶" & indentPattern & valueAsDict & "¶" & ") "
)
& Case( not IsEmpty( DictFirst( rest ) )
; " ¶&¶# ( " & DictToCalcString( rest; subLevel + 1 )
; ""
)
& If( subLevel = 0; "" )
)
)
)]]></Calculation></CustomFunction><CustomFunction id="14" functionArity="3" visible="True" parameters="dict;keys;outdelimiter" name="DictToDelimitedList"><Calculation><![CDATA[// DictToDelimitedList( dict; keys; outdelimiter )
// version 1.1, NYHTC
// Grabs the values from a dictionary object (as used by SixFriedRice.com) that belong to the key(s) (return-delimited) listed, separating each by the delimiter specified. If there are multiple ocurrences of a key, EACH value will be returned.
/*
VERSION HISTORY:
1.1 - Reformatted calc to match standards.
*/
Let( [
firstKeyName = DictFirst( dict )
];
If( Length( firstKeyName ) = 0
; ""
; Let( [
firstKeyIsWanted = ValueIsInList( keys; firstKeyName )
; restOfDict = DictRemove( dict; firstKeyName )
];
Case(
firstKeyIsWanted
; AppendWithSeparator ( DictGet( dict; firstKeyName ) ; DictToDelimitedList ( restOfDict; keys; outdelimiter ) ; outdelimiter )
; DictToDelimitedList ( restOfDict; keys; outdelimiter )
)
)
)
)]]></Calculation></CustomFunction><CustomFunction id="15" functionArity="2" visible="True" parameters="dict;subLevel" name="DictToJSON"><Calculation><![CDATA[// DictToJSON( dict; subLevel )
// version 1.1, Daniel A. Shockley
/* Displays a SixFriedRice.com-style Dictionary as JSON to make it easier to read by a human.
VERSION HISTORY:
1.1 - fixed a bug where there would be a trailing comma.
1.0 - initial version.
EXAMPLE USE:
If the variable $someDictionary is set to:
-----------------------
#( "FirstName"; "Bob" )
& #( "LastName"; "Smith" )
& #( "PhoneList"
; #( "Phone"
; #( "Label"; "Home" ) & #( "Value"; "212-123-4567" )
)
& #( "Phone"
; #( "Label"; "Cell" ) & #( "Value"; "212-999-9876" )
)
)
-----------------------
and thus $someDictionary looks like this in the debugger:
-----------------------
<:FirstName:=Bob:><:LastName:=Smith:><:PhoneList:=/</:Phone/:/=//<//:Label//://=Home//://>//<//:Value//://=212-123-4567//://>/:/>/</:Phone/:/=//<//:Label//://=Cell//://>//<//:Value//://=212-999-9876//://>/:/>:>
-----------------------
Using DictToJSON( $someDictionary; "" ) will give the much-easier-to-read display:
-----------------------
{
"FirstName": "Bob",
"LastName": "Smith",
"PhoneList":
{
"Phone":
{
"Label": "Home",
"Value": "212-123-4567"
},
"Phone":
{
"Label": "Cell",
"Value": "212-999-9876"
}
}
}
-----------------------
*/
Let( [
indentPattern = " " // character(s) to use as prefix for each level of indentation
; subLevel = If( IsEmpty( subLevel ); 0; subLevel ) // identifies what is current level of indent
; firstKey = DictFirst ( dict )
];
If( IsEmpty( firstKey )
; ""
; Let( [
rawData = DictGet( dict; firstKey )
; valueAsDict = If( IsEmpty( DictFirst( rawData ) ) ; ""; "¶" & indentPattern & Substitute( DictToJSON( rawData; subLevel + 1 ); "¶"; "¶" & indentPattern ) )
; rest = DictRemove( dict ; firstKey )
];
If( subLevel = 0; "{¶" )
& indentPattern
& If( IsEmpty( valueAsDict )
; Quote( firstKey ) & ": " & Quote( rawData )
; Quote( firstKey ) & ": ¶" & indentPattern & "{" & valueAsDict & "¶" & indentPattern & "}"
)
& Case( not IsEmpty( DictFirst( rest ) )
; ", ¶" & DictToJSON( rest; subLevel + 1 )
; ""
)
& If( subLevel = 0; "¶}" )
)
)
)]]></Calculation></CustomFunction><CustomFunction id="16" functionArity="1" visible="True" parameters="dict" name="DictToVariables"><Calculation><![CDATA[// DictToVariables( dict )
// version 1.1, Daniel A. Shockley, NYHTC, based on code examples from SixFriedRice.com blog comments.
// Converts a dictionary object (as used by SixFriedRice.com) into script variables (single $), scoped to exist within the script that calls this function. Returns a list of the variable names created.
/*
VERSION HISTORY:
1.1 - Reformatted to match standards.
*/
Let( [
firstKeyName = DictFirst( dict )
; makeVar = "Let( $"
& firstKeyName
& " = "
& Quote( DictGet( dict; firstKeyName ) )
& "; \"$"
& firstKeyName
& "\" )"
; nextKeyName = If( Length( firstKeyName ) > 0; DictFirst( DictRemove( dict; firstKeyName ) ); "" )
];
If( Length( firstKeyName ) > 0
; Evaluate( makeVar )
& If( Length( nextKeyName ) = 0
; ""
; "¶" & DictToVariables( DictRemove( dict; firstKeyName ) )
)
; ""
)
)]]></Calculation></CustomFunction><CustomFunction id="17" functionArity="1" visible="True" parameters="input" name="DictUnescape"><Calculation><![CDATA[// DictUnescape ( input )
// version 1.0, SixFriedRice.com
// Unescapes a name/value (aka key/value) string extracted from a SixFriedRice.com-style dictionary.
Substitute( input; ["/:"; ":"]; ["/="; "="] ; ["/>"; ">"] ; ["/<" ; "<" ] )]]></Calculation></CustomFunction></fmxmlsnippet>