@@ -3,6 +3,7 @@ import { minimalCloudFormationJoin } from './private/cloudformation-lang';
3
3
import { Intrinsic } from './private/intrinsic' ;
4
4
import { Reference } from './reference' ;
5
5
import { IResolvable , IResolveContext } from './resolvable' ;
6
+ import { Stack } from './stack' ;
6
7
import { captureStackTrace } from './stack-trace' ;
7
8
import { Token } from './token' ;
8
9
@@ -412,6 +413,37 @@ export class Fn {
412
413
return Token . asList ( new FnValueOfAll ( parameterType , attribute ) ) ;
413
414
}
414
415
416
+ /**
417
+ * The `Fn::ToJsonString` intrinsic function converts an object or array to its
418
+ * corresponding JSON string.
419
+ *
420
+ * @param object The object or array to stringify
421
+ */
422
+ public static toJsonString ( object : any ) : string {
423
+ // short-circut if object is not a token
424
+ if ( ! Token . isUnresolved ( object ) ) {
425
+ return JSON . stringify ( object ) ;
426
+ }
427
+ return new FnToJsonString ( object ) . toString ( ) ;
428
+ }
429
+
430
+ /**
431
+ * The intrinsic function `Fn::Length` returns the number of elements within an array
432
+ * or an intrinsic function that returns an array.
433
+ *
434
+ * @param array The array you want to return the number of elements from
435
+ */
436
+ public static len ( array : any ) : number {
437
+ // short-circut if array is not a token
438
+ if ( ! Token . isUnresolved ( array ) ) {
439
+ if ( ! Array . isArray ( array ) ) {
440
+ throw new Error ( 'Fn.length() needs an array' ) ;
441
+ }
442
+ return array . length ;
443
+ }
444
+ return Token . asNumber ( new FnLength ( array ) ) ;
445
+ }
446
+
415
447
private constructor ( ) { }
416
448
}
417
449
@@ -829,6 +861,62 @@ class FnJoin implements IResolvable {
829
861
}
830
862
}
831
863
864
+ /**
865
+ * The `Fn::ToJsonString` intrinsic function converts an object or array to its
866
+ * corresponding JSON string.
867
+ */
868
+ class FnToJsonString implements IResolvable {
869
+ public readonly creationStack : string [ ] ;
870
+
871
+ private readonly object : any ;
872
+
873
+ constructor ( object : any ) {
874
+ this . object = object ;
875
+ this . creationStack = captureStackTrace ( ) ;
876
+ }
877
+
878
+ public resolve ( context : IResolveContext ) : any {
879
+ Stack . of ( context . scope ) . addTransform ( 'AWS::LanguageExtensions' ) ;
880
+ return { 'Fn::ToJsonString' : this . object } ;
881
+ }
882
+
883
+ public toString ( ) {
884
+ return Token . asString ( this , { displayHint : 'Fn::ToJsonString' } ) ;
885
+ }
886
+
887
+ public toJSON ( ) {
888
+ return '<Fn::ToJsonString>' ;
889
+ }
890
+ }
891
+
892
+ /**
893
+ * The intrinsic function `Fn::Length` returns the number of elements within an array
894
+ * or an intrinsic function that returns an array.
895
+ */
896
+ class FnLength implements IResolvable {
897
+ public readonly creationStack : string [ ] ;
898
+
899
+ private readonly array : any ;
900
+
901
+ constructor ( array : any ) {
902
+ this . array = array ;
903
+ this . creationStack = captureStackTrace ( ) ;
904
+ }
905
+
906
+ public resolve ( context : IResolveContext ) : any {
907
+ Stack . of ( context . scope ) . addTransform ( 'AWS::LanguageExtensions' ) ;
908
+ return { 'Fn::Length' : this . array } ;
909
+ }
910
+
911
+ public toString ( ) {
912
+ return Token . asString ( this , { displayHint : 'Fn::Length' } ) ;
913
+ }
914
+
915
+ public toJSON ( ) {
916
+ return '<Fn::Length>' ;
917
+ }
918
+ }
919
+
832
920
function _inGroupsOf < T > ( array : T [ ] , maxGroup : number ) : T [ ] [ ] {
833
921
const result = new Array < T [ ] > ( ) ;
834
922
for ( let i = 0 ; i < array . length ; i += maxGroup ) {
@@ -843,4 +931,4 @@ function range(n: number): number[] {
843
931
ret . push ( i ) ;
844
932
}
845
933
return ret ;
846
- }
934
+ }
0 commit comments