-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.coffee
2522 lines (1768 loc) · 63.7 KB
/
functions.coffee
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
_ = require('underscore')
_.str = require('underscore.string')
_.mixin(_.str.exports())
turfAlong = require('@turf/along').default
turfArea = require('@turf/area').default
turfBearing = require('@turf/bearing').default
turfBooleanWithin = require('@turf/boolean-within').default
turfBooleanIntersects = require('@turf/boolean-intersects').default
turfBuffer = require('@turf/buffer').default
turfConvex = require('@turf/convex').default
turfCentroid = require('@turf/centroid').default
turfDistance = require('@turf/distance').default
turfNearestPoint = require('@turf/nearest-point').default
turfNearestPointOnLine = require('@turf/nearest-point-on-line').default
turfHelpers = require('@turf/helpers')
turfLength = require('@turf/length').default
turfTag = require('@turf/tag').default
inspect = require('object-inspect')
qs = require('query-string')
encodeUrl = require('encodeurl')
Utils = require('./utils')
{format} = require('util')
toArray = Utils.toArray
Defaults =
locale: 'en_US'
language: 'en-US'
country: 'US'
currencyCode: 'USD'
currencySymbol: '$'
timeZone: 'UTC'
decimalSeparator: '.'
groupingSeparator: ','
groupingSize: 3
Config = _.extend({}, Defaults)
exports = {}
exports.NO_VALUE = undefined
exports.ERROR = (message) ->
throw new Error(message)
exports.MATH_FUNC = MATH_FUNC = (mathFunction) ->
-> mathFunction.apply(Math, toArray(arguments).map(NUM))
exports.ABS = MATH_FUNC(Math.abs)
exports.ACOS = MATH_FUNC(Math.acos)
exports.ACOSH = (number) ->
number = NUM(number)
Math.log(number + Math.sqrt(number * number - 1))
exports.ALERT = ->
title = null
message = arguments[0]
if arguments.length > 1
title = arguments[0]
message = arguments[1]
result =
type: 'message'
title: if title? then title.toString() else null
message: if message? then message.toString() else null
$$runtime.results.push(result)
exports.ALTITUDE = ->
NUM(CONFIG().recordAltitude)
exports.AND = ->
_.find(toArray(arguments), (item) -> not item) is undefined
exports.APPLICATION = ->
Config.application ? ''
exports.APPLICATIONBUILD = ->
Config.applicationBuild ? ''
exports.APPLICATIONINFO = (separator=', ') ->
_.compact([ APPLICATION(), APPLICATIONVERSION(), APPLICATIONBUILD() ]).join(separator)
exports.APPLICATIONVERSION = ->
Config.applicationVersion ? ''
evaluateChoiceValueEquals = (choiceValues, matchValues) ->
Array.isArray(choiceValues) and
Array.isArray(matchValues) and
choiceValues.length is matchValues.length and
choiceValues.every((element) -> element in matchValues)
evaluateEquals = (field, value) ->
result = switch FIELDTYPE(field)
when 'ChoiceField', 'ClassificationField' then evaluateChoiceValueEquals(CHOICEVALUES(VALUE(field)), value)
else VALUE(field) is value
return result
evaluateCondition = ({ field, operator, value }) ->
return false if not field or not operator
result = switch operator
when 'equals' then evaluateEquals(field, value)
else false
return result
performAction = (action) ->
return if !action
switch action.type
when 'setvalue' then SETVALUE(action.field, action.value)
applyEffect = ({ actions, conditions }) ->
return if not Array.isArray(actions) or not Array.isArray(conditions)
actions.forEach(performAction) if conditions.every(evaluateCondition)
createApplyEffectCallback = (effect) ->
return if !effect or !effect.event or !effect.event.name
if effect.event.field
ON(effect.event.name, effect.event.field, (event) -> applyEffect(effect))
else
ON(effect.event.name, (event) -> applyEffect(effect))
exports.APPLYFIELDEFFECTS = (fieldEffects) ->
return if !fieldEffects or not Array.isArray(fieldEffects.effects)
createApplyEffectCallback(effect) for effect in fieldEffects.effects
exports.ARRAY = ->
FLATTEN(toArray(arguments))
exports.AVERAGE = ->
args = ARRAY(toArray(arguments))
return NaN if args.length is 0
_.inject(args, ((memo, arg) -> memo + +arg), 0) / args.length
exports.CEILING = (number, significance = 1) ->
significance ?= 1
significance = ABS(significance)
number = NUM(number)
return NaN if isNaN(number) or isNaN(significance)
return 0 if significance is 0
precision = PRECISION(significance)
if number >= 0
ROUND(Math.ceil(number / significance) * significance, precision)
else
-ROUND(Math.floor(Math.abs(number) / significance) * significance, precision)
exports.CHAR = (number) ->
number = NUM(number)
String.fromCharCode number
exports.CHOICEVALUE = (field) ->
values = CHOICEVALUES(field)
return NO_VALUE unless _.isArray(values)
return NO_VALUE if values.length is 0
values[0]
exports.CHOICEVALUES = (field) ->
return NO_VALUE unless field?.choice_values? or field?.other_values?
return [] if ISBLANK(field)
values = [].concat(field.choice_values).concat(field.other_values)
_.compact(values)
CLEAN_REGEX = /[\x00\x08\x0B\x0C\x0E-\x1F]/g
exports.CLEAN = (text) ->
text ?= ''
text.replace CLEAN_REGEX, ''
exports.COALESCE = ->
if arguments.length is 1 and _.isArray(arguments[0])
COALESCE.apply(null, arguments[0])
else
(_.find toArray(arguments), (value) -> value?) ? NO_VALUE
exports.CODE = (string) ->
string = string.toString() if _.isNumber(string)
return NaN unless _.isString(string)
(string ? '').charCodeAt(0)
# _.compact removes '' and 0 from the array, which is somewhat unexpected
exports.COMPACT = (value) ->
return NO_VALUE unless _.isArray(value)
_.filter value, (item) -> item?
exports.CONCATENATE = ->
strings = _.map ARRAY(toArray(arguments)), (arg) ->
switch true
when _.isString(arg)
arg
when _.isNumber(arg)
'' + arg
else
''
strings.join('')
exports.CONCAT = exports.CONCATENATE
exports.CONFIG = ->
Config
exports.CONFIRM = ->
title = null
message = arguments[0]
callback = arguments[1]
if arguments.length > 2
title = arguments[0]
message = arguments[1]
callback = arguments[2]
buttons = ['Cancel', 'Okay']
MESSAGEBOX(title: title, message: message, buttons: buttons, callback)
exports.CONFIGURE = (config, merge=true) ->
if merge
_.extend(Config, config)
else
Config = config
Config
exports.CONTAINS = (haystack, needle, fromIndex=0) ->
fromIndex = 0 unless _.isNumber(fromIndex)
return false unless _.isString(haystack) or _.isArray(haystack)
if _.isString(haystack)
haystack.indexOf(needle.toString(), fromIndex) isnt -1
else
_.contains(haystack, needle, fromIndex)
exports.COS = MATH_FUNC(Math.cos)
exports.COSH = (number) ->
number = NUM(number)
exp = Math.exp(number)
(exp + 1 / exp) / 2
exports.COUNT = (value) ->
return NO_VALUE unless _.isArray(value)
numbers = _.select COMPACT(value).map(NUM), ISNUMBER
numbers.length
exports.COUNTA = (value) ->
values = _.select ARRAY(toArray(arguments)), EXISTS
values.length
exports.COUNTBLANK = (value) ->
results = _.filter ARRAY(toArray(arguments)), (item) ->
switch true
when not item?
true
when _.isArray(item)
item.length is 0
when _.isString(item)
_.isBlank(item)
else
false
results.length
exports.COUNTRY = ->
Config.country or Defaults.country
exports.CURRENCYCODE = ->
Config.currencyCode or Defaults.currencyCode
exports.CURRENCYSYMBOL = ->
Config.currencySymbol or Defaults.currencySymbol
exports.CURRENTLOCATION = ->
$$runtime.currentLocation ? null
exports.DATANAMES = (type) ->
elements =
if type?
_.filter $$runtime.elements, (e) -> e.type is type
else
$$runtime.elements
elements.map (e) -> e.data_name
exports.DATE = (year, month, day) ->
year = INT(year)
month = INT(month)
day = INT(day)
return NO_VALUE if ISNAN(year) or ISNAN(month) or ISNAN(day)
new Date("#{year}/#{month}/#{day} 00:00:00")
exports.DATEADD = (date, number, type='day') ->
date = DATEVALUE(date)
number = INT(number)
return NO_VALUE unless date?
return NO_VALUE if ISNAN(number)
return NO_VALUE unless type is 'day'
time = date.getTime()
time += (number * (1000 * 60 * 60 * 24))
new Date(time)
exports.DATEVALUE = (dateString, timeString) ->
if _.isDate(dateString)
year = dateString.getFullYear()
month = LPAD(dateString.getMonth() + 1, 2, '0')
day = LPAD(dateString.getDate(), 2, '0')
dateString = year + '-' + month + '-' + day
return NO_VALUE unless _.isString(dateString)
timeString = '00:00:00' unless _.isString(timeString)
timeString = timeString + ':00' if timeString.length is 5
date = null
if dateString.length <= 10
dateString = dateString.replace(/-/g, '/')
date = new Date(dateString + ' ' + timeString)
else
date = new Date(dateString)
return NO_VALUE if ISNAN(date.getTime())
date
exports.DAY = (date) ->
date = DATEVALUE(date)
return NO_VALUE unless date?
date.getDate()
exports.DECIMALSEPARATOR = ->
Config.decimalSeparator or Defaults.decimalSeparator
exports.DEGREES = (value) ->
value = NUM(value)
return NaN unless _.isNumber(value)
180.0 * value / Math.PI
exports.DESCRIPTION = (dataName) ->
field = FIELD(dataName)
return unless field?
field.description
exports.DEVICEINFO = (separator=', ') ->
_.compact([ DEVICEMANUFACTURER(), DEVICEMODEL() ]).join(separator)
exports.DEVICEMODEL = ->
Config.deviceModel ? ''
exports.DEVICEMANUFACTURER = ->
Config.deviceManufacturer ? ''
exports.DOLLAR = (value, decimals=2, currency=null, language=null) ->
decimals = NUM(decimals)
decimals ?= 2
decimals = 2 if ISNAN(decimals)
value = NUM(value)
return NO_VALUE unless _.isNumber(value)
currency ?= CURRENCYCODE()
language ?= LANGUAGE()
options =
style: 'currency'
currency: currency
minimumFractionDigits: decimals
maximumFractionDigits: decimals
FORMATNUMBER(value, language, options)
exports.EMAIL = ->
CONFIG().userEmail ? NO_VALUE
exports.EVEN = (value) ->
value = NUM(value)
return NaN unless _.isNumber(value)
CEILING(value, -2, -1)
exports.EXACT = (value1, value2) ->
_.isEqual(value1, value2)
exports.EXISTS = (value) ->
_.select(toArray(arguments).map(ISBLANK)).length is 0
exports.EXP = MATH_FUNC(Math.exp)
exports.FIELD = (dataName) ->
element = $$runtime.elementsByDataName[dataName]
return NO_VALUE unless element?
element
exports.FIELDS = (dataName, options = {}) ->
element = FIELD(dataName)
options ?= {}
options.repeatables ?= true
options.sections ?= true
return NO_VALUE unless element?
return NO_VALUE unless element.elements?
return Utils.flattenElements(element.elements, options.repeatables, false, null, options.sections)
exports.FIELDNAMES = (dataName, options = {}) ->
fields = FIELDS(dataName, options)
return NO_VALUE unless fields?
fields.map (o) => o.data_name
exports.FIELDTYPE = (dataName) ->
field = FIELD(dataName)
return NO_VALUE unless field?
field.type
exports.FIRST = (array, count) ->
_.first(array, count)
exports.MEMOIZED_FACT = []
exports.FACT = (value) ->
value = NUM(value)
return NaN if ISNAN(value)
return NaN if value < 0
n = Math.floor(value)
if n is 0 or n is 1
1
else if MEMOIZED_FACT[n] > 0
MEMOIZED_FACT[n]
else
MEMOIZED_FACT[n] = FACT(n - 1) * n
MEMOIZED_FACT[n]
exports.MEMOIZED_FACTDOUBLE = []
exports.FACTDOUBLE = (value) ->
value = NUM(value)
return NaN if ISNAN(value)
return NaN if value < 0
n = Math.floor(value)
if n <= 1
1
else if MEMOIZED_FACTDOUBLE[n] > 0
MEMOIZED_FACTDOUBLE[n]
else
MEMOIZED_FACTDOUBLE[n] = FACTDOUBLE(n - 2) * n
MEMOIZED_FACTDOUBLE[n]
exports.FALSE = ->
false
exports.FIND = (needle, haystack, position) ->
position = NUM(position)
position = 0 if ISNAN(position)
return NO_VALUE unless haystack and haystack.indexOf
return NO_VALUE if _.isArray(needle)
index = haystack.indexOf(needle, position - 1)
return NO_VALUE if index < 0
index + 1
exports.FIXED = (number, decimals=2, suppressGroupingSeparator=false) ->
number = NUM(number)
decimals = NUM(decimals)
decimals = 2 if ISNAN(decimals)
decimals = MAX(decimals, 0)
decimals = MIN(decimals, 20)
return NO_VALUE if ISNAN(number)
return NO_VALUE if ISNAN(decimals)
suppressGroupingSeparator = !!suppressGroupingSeparator
power = Math.pow(10, decimals)
scaled = parseInt(number * power)
machineDecimalSeparator = '.'
machineValue = number.toFixed(decimals)
index = machineValue.indexOf(machineDecimalSeparator)
return machineValue unless index > -1
integerPart = parseInt(machineValue.substring(0, index))
fractionPart = machineValue.substring(index + 1, machineValue.length)
integerString = ''
groupingSize = GROUPINGSIZE()
groupingMax = Math.pow(10, groupingSize)
if suppressGroupingSeparator or integerPart < groupingMax
integerString = integerPart.toString()
else
groupingSeparator = GROUPINGSEPARATOR()
parts = []
while integerPart >= groupingMax
thisIntegerPart = Math.floor(integerPart % groupingMax)
zeroPadding = new Array(groupingSize + 1).join('0')
parts.push(RIGHT(zeroPadding + thisIntegerPart.toString(), groupingSize))
integerPart = Math.floor(integerPart / groupingMax)
if integerPart > 0
parts.push(integerPart.toString())
integerString = parts.reverse().join(groupingSeparator)
if decimals < 1
integerString
else
integerString + DECIMALSEPARATOR() + fractionPart.toString()
exports.FLATTEN = (value) ->
return NO_VALUE unless _.isArray(value)
_.flatten value
exports.FLOOR = (number, significance) ->
significance ?= 1
significance = ABS(significance)
number = NUM(number)
return NaN if ISNAN(number) or ISNAN(significance)
return 0 if significance is 0
precision = PRECISION(significance)
if number >= 0
ROUND(Math.floor(number / significance) * significance, precision)
else
-ROUND(Math.ceil(Math.abs(number) / significance) * significance, precision)
exports.FORM = ->
$$runtime.form
exports.FORMAT = ->
format.apply(null, arguments)
exports.FORMATADDRESS = (address, {partSeparator, lineSeparator} = {}) ->
return NO_VALUE unless address?
lineSeparator ?= '\n'
partSeparator ?= ' '
formatLine = (parts...) =>
components = []
for part in parts
components.push(part) if EXISTS(part)
components.join(partSeparator)
line1 = formatLine(address.sub_thoroughfare, address.thoroughfare, address.suite and '#' + address.suite)
line2 = formatLine(address.locality, address.admin_area, address.postal_code)
line3 = formatLine(address.country)
lines = []
lines.push(line1) if EXISTS(line1)
lines.push(line2) if EXISTS(line2)
lines.push(line3) if EXISTS(line3)
lines.join(lineSeparator)
exports.FORMATNUMBER = (number, language, options) ->
number ?= NUM(number)
language ?= LANGUAGE()
options ?= {}
style = 'decimal'
style = 'currency' if options.style is 'currency'
style = 'percent' if options.style is 'percent'
options.style = style
if options.style is 'currency'
options.currency ?= CURRENCYCODE()
hasSignificantDigitsOption = _.isNumber(options.minimumSignificantDigits) or _.isNumber(options.maximumSignificantDigits)
if hasSignificantDigitsOption
options.minimumSignificantDigits ?= 1
options.minimumSignificantDigits = NUM(options.minimumSignificantDigits)
options.minimumSignificantDigits = MIN(MAX(options.minimumSignificantDigits, 1), 21)
options.maximumSignificantDigits ?= options.minimumSignificantDigits
options.maximumSignificantDigits = NUM(options.maximumSignificantDigits)
options.maximumSignificantDigits = MIN(MAX(options.maximumSignificantDigits, 1), 21)
else
options.minimumIntegerDigits ?= 1
options.minimumIntegerDigits = NUM(options.minimumIntegerDigits)
options.minimumIntegerDigits = MIN(MAX(options.minimumIntegerDigits, 1), 21)
options.minimumFractionDigits ?= if options.style is 'currency' then 2 else 0
options.minimumFractionDigits = NUM(options.minimumFractionDigits)
options.minimumFractionDigits = MIN(MAX(options.minimumFractionDigits, 0), 20)
if options.style is 'currency'
options.maximumFractionDigits ?= 2
else if options.style is 'percent'
options.maximumFractionDigits ?= MAX(options.minimumFractionDigits, 0)
else
options.maximumFractionDigits ?= MAX(options.minimumFractionDigits, 3)
options.maximumFractionDigits = NUM(options.maximumFractionDigits)
options.maximumFractionDigits = MIN(MAX(options.maximumFractionDigits, 0), 20)
unless _.isBoolean(options.useGrouping)
options.useGrouping = true
HostFunctions.formatNumber(number, language, options)
exports.GCD = ->
numbers = toArray(arguments).map(NUM)
count = numbers.length
return NaN if numbers.length is 0
return NaN if numbers[0] < 0
result = numbers[0]
for i in [1..count - 1]
return NaN if numbers[i] < 0
num = numbers[i]
while result and num
if result > num
result %= num
else
num %= result
result += num
result
exports.GEOMETRYALONG = (line, distance, options) ->
turfAlong(line, distance, options)
exports.GEOMETRYAREA = (geometry) ->
turfArea(geometry)
exports.GEOMETRYBEARING = (start, end, options) ->
turfBearing(start, end, options)
exports.GEOMETRYBUFFER = (geometry, radius, options) ->
turfBuffer(geometry, radius, options)
exports.GEOMETRYCENTROID = (geometry) ->
turfCentroid(geometry)
exports.GEOMETRYCONVEX = (geojson, options) ->
turfConvex(geojson, options)
exports.GEOMETRYDISTANCE = (fromPoint, toPoint, options) ->
turfDistance(fromPoint, toPoint, options)
exports.GEOMETRYFEATURE = (geometry, proprties, options) ->
turfHelpers.feature(geometry, proprties, options)
exports.GEOMETRYFEATURECOLLECTION = (features, options) ->
turfHelpers.featureCollection(features, options)
exports.GEOMETRYLENGTH = (geometry, options) ->
turfLength(geometry, options)
exports.GEOMETRYLINESTRING = (coordinates, properties, options) ->
turfHelpers.lineString(coordinates, properties, options)
exports.GEOMETRYNEARESTPOINT = (targetPoint, points) ->
turfNearestPoint(targetPoint, points)
exports.GEOMETRYNEARESTPOINTONLINE = (lines, point, options) ->
turfNearestPointOnLine(lines, point, options)
exports.GEOMETRYPOINT = (coordinates, properties, options) ->
turfHelpers.point(coordinates, properties, options)
exports.GEOMETRYPOLYGON = (coordinates, properties, options) ->
turfHelpers.polygon(coordinates, properties, options)
exports.GEOMETRYTAG = (points, polygons, field, outField) ->
turfTag(points, polygons, field, outField)
exports.GEOMETRYINTERSECTS = (feature1, feature2) ->
turfBooleanIntersects(feature1, feature2)
exports.GEOMETRYWITHIN = (feature1, feature2) ->
turfBooleanWithin(feature1, feature2)
exports.GETRESULT = ->
$$runtime.$$result
exports.REQUEST = (options, callback) ->
return ERROR('A callback must be provided to REQUEST') unless _.isFunction(callback)
if _.isString(options)
options = { url: options }
options.method ?= 'GET'
options.headers ?= {}
options.followRedirect ?= true
return ERROR('A url must be provided to REQUEST') unless _.isString(options.url)
if _.isObject(options.qs)
queryString = qs.stringify(options.qs)
if options.url.indexOf('?') < 0
queryString = '?' + queryString
options.url += queryString
delete options.qs
if options.json?
options.headers['Content-Type'] = 'application/json'
unless _.isString(options.json)
options.body = JSON.stringify(options.json)
options.method = 'GET' unless _.isString(options.method)
options.headers = {} unless _.isObject(options.headers)
options.body = null unless _.isString(options.body)
options.followRedirect = !!options.followRedirect
# Encode the entire URL. This allows sloppy inputs with partially encoded params
# and prevents double encoding.
options.url = encodeUrl(options.url)
HostFunctions.httpRequest(JSON.stringify(options), callback)
exports.GROUP = ->
args = ARRAY(toArray(arguments))
return NO_VALUE if args.length is 0
callback = null
values = args
if _.isFunction(_.last(args))
callback = _.last(args)
values = _.first(args, args.length - 1)
_.groupBy(values, callback)
exports.GROUPINGSEPARATOR = ->
Config.groupingSeparator or Defaults.groupingSeparator
exports.GROUPINGSIZE = ->
Config.groupingSize or Defaults.groupingSize
exports.HASOTHER = (value) ->
!!(value and
value.other_values and
_.isArray(value.other_values) and
value.other_values.length > 0)
exports.IF = (test, trueValue, falseValue) ->
value = falseValue
if test
value = trueValue
if TYPEOF(value) == 'function'
value = value()
value
exports.IFERROR = (value, errorValue) ->
if ISERR(value) then errorValue else value
exports.INSPECT = (value) ->
inspect(value)
exports.INT = exports.FLOOR
exports.INVALID = ->
key = null
message = null
if arguments.length > 1
element = FIELD(arguments[0])
key = element?.key or arguments[0].toString()
message = arguments[1].toString()
else if arguments.length is 1
message = arguments[0].toString()
result =
type: 'validation'
key: key
message: message
$$runtime.results.push(result)
exports.ISBLANK = (value) ->
return true unless value?
return true if _.isNaN(value)
return false if _.isBoolean(value)
return false if _.isNumber(value)
return false if _.isDate(value)
return false if _.isRegExp(value)
return _.isBlank(value) if _.isString(value)
return value.length is 0 if _.isArray(value)
if value and (value.choice_values or
value.choice_values is null or
value.other_values or
value.other_values is null)
choice = value.choice_values
others = value.other_values
hasChoice = _.isArray(choice) and choice.length > 0
hasOthers = _.isArray(others) and others.length > 0
hasEither = hasChoice or hasOthers
return not hasEither
Object.keys(value).length is 0
exports.ISERR = (value) ->
return true unless value?
return true if isNaN(value)
return true if value instanceof Error
false
exports.ISERROR = (value) ->
ISERR(value)
exports.ISLOGICAL = (value) ->
_.isBoolean(value)
exports.ISNAN = (value) ->
not ISNUMBER(value)
exports.ISEVEN = (value) ->
value = NUM(value)
return false unless _.isNumber(value)
return false if ISNAN(value)
(Math.floor(Math.abs(value)) & 1) is 0
exports.ISLANDSCAPE = (media) ->
return not ISPORTRAIT(media)
exports.ISMOBILE = ->
CONTAINS(['iOS', 'Android'], PLATFORM())
exports.ISNEW = ->
CONFIG().featureIsNew is true
exports.ISNONTEXT = (value) ->
not _.isString(value)
exports.ISNUMBER = (value) ->
_.isFinite(NUM(value))
exports.ISODD = (value) ->
value = NUM(value)
return false unless _.isNumber(value)
return false if ISNAN(value)
(Math.floor(Math.abs(value)) & 1) is 1
exports.ISPORTRAIT = (media) ->
return NO_VALUE unless media?
width = media.width
height = media.height
# photos
if media.orientation is 6 or media.orientation is 8
width = media.height
height = media.width
# videos
if media.orientation is 90 or media.orientation is 270
width = media.height
height = media.width
return width <= height
exports.ISROLE = ->
CONTAINS(ARRAY(toArray(arguments)), ROLE())
exports.ISSELECTED = (value, choice) ->
return false if ISBLANK(value)
return false unless choice?
if _.isArray(choice)
return (choice.filter (item) -> not ISSELECTED(value, item)).length is 0
if value and value.choice_values
return true if _.contains(value.choice_values, choice)
if value and value.other_values
return true if _.contains(value.other_values, choice)
false
exports.ISTEXT = (value) ->
_.isString(value)
exports.ISUPDATE = ->
not ISNEW()
exports.LABEL = (dataName) ->
field = FIELD(dataName)
return unless field?
field.label
exports.LANGUAGE = ->
Config.language or Defaults.language
exports.LAST = (array, count) ->
_.last(array, count)