@adobe/acc-js-sdk 1.0.7 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/util.test.js CHANGED
@@ -17,7 +17,7 @@ governing permissions and limitations under the License.
17
17
  *
18
18
  *********************************************************************************/
19
19
 
20
- const { Util } = require('../src/util.js');
20
+ const { Util, ArrayMap } = require('../src/util.js');
21
21
  const { SafeStorage, Cache } = require('../src/cache.js');
22
22
 
23
23
 
@@ -232,4 +232,170 @@ describe('Util', function() {
232
232
  expect(cache._cache["Hello"].value).toBe("World");
233
233
  })
234
234
 
235
+ describe("ArrayMap", () => {
236
+
237
+ it("Should support access by keys", () => {
238
+ const am = new ArrayMap();
239
+ am._push("hello", "Hello");
240
+ am._push("world", "World");
241
+ expect(am["hello"]).toBe("Hello");
242
+ expect(am["world"]).toBe("World");
243
+ expect(am.get("hello")).toBe("Hello");
244
+ expect(am.get("world")).toBe("World");
245
+ });
246
+
247
+ it("Should support access by index", () => {
248
+ const am = new ArrayMap();
249
+ am._push("hello", "Hello");
250
+ am._push("world", "World");
251
+ expect(am[0]).toBe("Hello");
252
+ expect(am[1]).toBe("World");
253
+ expect(am.get(0)).toBe("Hello");
254
+ expect(am.get(1)).toBe("World");
255
+ });
256
+
257
+ it("Should support length attribute", () => {
258
+ const am = new ArrayMap();
259
+ am._push("hello", "Hello");
260
+ am._push("world", "World");
261
+ expect(am.length).toBe(2);
262
+ });
263
+
264
+ it("Should support iterators (for...of)", () => {
265
+ const am = new ArrayMap();
266
+ am._push("hello", "Hello");
267
+ am._push("world", "World");
268
+ let cat = "";
269
+ for (const s of am) cat = cat + s;
270
+ expect(cat).toBe("HelloWorld");
271
+ });
272
+
273
+ it("Should support map()", () => {
274
+ const am = new ArrayMap();
275
+ am._push("hello", "Hello");
276
+ am._push("world", "World");
277
+ const cat = am.map(s => s).join(',');
278
+ expect(cat).toBe("Hello,World");
279
+ });
280
+
281
+ it("Should support flatMap()", () => {
282
+ const am = new ArrayMap();
283
+ am._push("hello", "Hello");
284
+ am._push("world", ["Adobe", "World"]);
285
+ const cat = am.flatMap(s => s).join(',');
286
+ expect(cat).toBe("Hello,Adobe,World");
287
+ });
288
+
289
+ it("Should support find()", () => {
290
+ const am = new ArrayMap();
291
+ am._push("hello", "Hello");
292
+ am._push("world", "World");
293
+ const world = am.find(s => s === 'World');
294
+ expect(world).toBe("World");
295
+ const notFound = am.find(s => s === 'NotFound');
296
+ expect(notFound).toBe(undefined);
297
+ });
298
+
299
+ it("Should support filter()", () => {
300
+ const am = new ArrayMap();
301
+ am._push("hello", "Hello");
302
+ am._push("world", "World");
303
+ const all = am.filter(s => true);
304
+ expect(all).toMatchObject([ "Hello", "World" ]);
305
+ const none = am.filter(s => false);
306
+ expect(none).toMatchObject([ ]);
307
+ const world = am.filter(s => s === 'World');
308
+ expect(world).toMatchObject([ "World" ]);
309
+ });
310
+
311
+ it("Should support forEach", () => {
312
+ const am = new ArrayMap();
313
+ am._push("hello", "Hello");
314
+ am._push("world", "World");
315
+ let cat = "";
316
+ am.forEach(s => cat = cat + s);
317
+ expect(cat).toBe("HelloWorld");
318
+ });
319
+
320
+ it("Should support forEach as a key", () => {
321
+ const am = new ArrayMap();
322
+ am._push("forEach", "Hello");
323
+ const cat = am.map(s => s).join(',');
324
+ expect(cat).toBe("Hello");
325
+ expect(typeof am.forEach).toBe('function');
326
+ expect(am["forEach"]).not.toBe("Hello"); // forEach is a function
327
+ expect(am.get("forEach")).toBe("Hello");
328
+ });
329
+
330
+ it("Should support for...in", () => {
331
+ const am = new ArrayMap();
332
+ am._push("hello", "Hello");
333
+ am._push("world", "World");
334
+ let cat = "";
335
+ for (const s in am) cat = cat + s;
336
+ expect(cat).toBe("helloworld");
337
+ });
338
+
339
+ it("Should not support for...in when there's a property named 'forEach'", () => {
340
+ const am = new ArrayMap();
341
+ am._push("hello", "Hello");
342
+ am._push("forEach", "World");
343
+ let cat = "";
344
+ for (const s in am) cat = cat + s;
345
+ expect(cat).toBe("hello");
346
+ });
347
+
348
+ it("Should support enumerations whose key is a number", () => {
349
+ // For instance the "addressQuality" enumeration
350
+ const am = new ArrayMap();
351
+ am._push("0", { name:"0", value:0 });
352
+ am._push("1", { name:"1", value:1 });
353
+ am._push("2", { name:"2", value:2 });
354
+ let cat = "";
355
+ for (const k in am) cat = cat + am.get(k).name;
356
+ expect(cat).toBe("012");
357
+ });
358
+
359
+ it("Should not support adding the same key twice", () => {
360
+ const am = new ArrayMap();
361
+ am._push("hello", "Hello");
362
+ expect(() => { am._push("hello", "World"); }).toThrow("Failed to add element 'hello' to ArrayMap. There's already an item with the same name");
363
+ });
364
+
365
+ it("Should support missing names", () => {
366
+ const am = new ArrayMap();
367
+ am._push("", { name:"0", value:0 });
368
+ am._push(undefined, { name:"1", value:1 });
369
+ am._push(null, { name:"2", value:2 });
370
+ expect(am.length).toBe(3);
371
+ expect(am[0].name).toBe("0");
372
+ expect(am[1].name).toBe("1");
373
+ expect(am[2].name).toBe("2");
374
+ });
375
+
376
+ it("Should handle compatibility", () => {
377
+ const am = new ArrayMap();
378
+ am._push("perfect", { name:"perfect", value:0 });
379
+ am._push("notPerfect", { name:"notPerfect", value:1 });
380
+ am._push("error", { name:"error", value:2 });
381
+ // length
382
+ expect(am.length).toBe(3);
383
+ // Access by name
384
+ expect(am.perfect).toMatchObject({ name:"perfect", value:0 });
385
+ expect(am.notPerfect).toMatchObject({ name:"notPerfect", value:1 });
386
+ expect(am.error).toMatchObject({ name:"error", value:2 });
387
+ expect(am.notFound).toBeUndefined();
388
+ // for .. in loop
389
+ const list = [];
390
+ for (const p in am) list.push(p);
391
+ expect(list).toMatchObject([ "perfect", "notPerfect", "error" ]);
392
+ });
393
+ });
394
+
395
+ describe("Is Browser", () => {
396
+ it("Should not be a browser", () => {
397
+ expect(Util.isBrowser()).toBe(false);
398
+ });
399
+ });
235
400
  });
401
+
@@ -428,7 +428,9 @@ describe('XtkCaster', function() {
428
428
  const value = expected[0];
429
429
  const expectedResult = expected[1];
430
430
  it('Should return the value casted as a timestamp ("' + value + '")', function() {
431
- const actual = XtkCaster.asTimestamp(value);
431
+ let actual = XtkCaster.asTimestamp(value);
432
+ assert.myEquals(actual, expectedResult);
433
+ actual = XtkCaster.asDatetime(value);
432
434
  assert.myEquals(actual, expectedResult);
433
435
  });
434
436
  it('Should return the value casted as type datetime', function() {
@@ -813,4 +815,123 @@ describe('XtkCaster', function() {
813
815
  });
814
816
  })
815
817
 
818
+ it("Should check time type", () => {
819
+ expect(XtkCaster.isTimeType(null)).toBe(false);
820
+ expect(XtkCaster.isTimeType(undefined)).toBe(false);
821
+ expect(XtkCaster.isTimeType(0)).toBe(false);
822
+ expect(XtkCaster.isTimeType("")).toBe(false);
823
+ expect(XtkCaster.isTimeType(6)).toBe(false);
824
+ expect(XtkCaster.isTimeType("string")).toBe(false);
825
+ expect(XtkCaster.isTimeType("int64")).toBe(false);
826
+ expect(XtkCaster.isTimeType("uuid")).toBe(false);
827
+ expect(XtkCaster.isTimeType(12)).toBe(false);
828
+ expect(XtkCaster.isTimeType(13)).toBe(false);
829
+ expect(XtkCaster.isTimeType("memo")).toBe(false);
830
+ expect(XtkCaster.isTimeType("CDATA")).toBe(false);
831
+ expect(XtkCaster.isTimeType("blob")).toBe(false);
832
+ expect(XtkCaster.isTimeType("html")).toBe(false);
833
+ expect(XtkCaster.isTimeType(1)).toBe(false);
834
+ expect(XtkCaster.isTimeType(2)).toBe(false);
835
+ expect(XtkCaster.isTimeType(3)).toBe(false);
836
+ expect(XtkCaster.isTimeType(15)).toBe(false);
837
+ expect(XtkCaster.isTimeType("byte")).toBe(false);
838
+ expect(XtkCaster.isTimeType("short")).toBe(false);
839
+ expect(XtkCaster.isTimeType("long")).toBe(false);
840
+ expect(XtkCaster.isTimeType("int")).toBe(false);
841
+ expect(XtkCaster.isTimeType("boolean")).toBe(false);
842
+ expect(XtkCaster.isTimeType(4)).toBe(false);
843
+ expect(XtkCaster.isTimeType(5)).toBe(false);
844
+ expect(XtkCaster.isTimeType("float")).toBe(false);
845
+ expect(XtkCaster.isTimeType("double")).toBe(false);
846
+ expect(XtkCaster.isTimeType(7)).toBe(true);
847
+ expect(XtkCaster.isTimeType(10)).toBe(true);
848
+ expect(XtkCaster.isTimeType("datetime")).toBe(true);
849
+ expect(XtkCaster.isTimeType("timestamp")).toBe(true);
850
+ expect(XtkCaster.isTimeType("datetimetz")).toBe(true);
851
+ expect(XtkCaster.isTimeType("datetimenotz")).toBe(true);
852
+ expect(XtkCaster.isTimeType("date")).toBe(true);
853
+ expect(XtkCaster.isTimeType(14)).toBe(true);
854
+ expect(XtkCaster.isTimeType("time")).toBe(true);
855
+ expect(XtkCaster.isTimeType("timespan")).toBe(true);
856
+ });
857
+
858
+ it("Should check string type", () => {
859
+ expect(XtkCaster.isStringType(null)).toBe(false);
860
+ expect(XtkCaster.isStringType(undefined)).toBe(false);
861
+ expect(XtkCaster.isStringType(0)).toBe(false);
862
+ expect(XtkCaster.isStringType("")).toBe(false);
863
+ expect(XtkCaster.isStringType(6)).toBe(true);
864
+ expect(XtkCaster.isStringType("string")).toBe(true);
865
+ expect(XtkCaster.isStringType("int64")).toBe(false);
866
+ expect(XtkCaster.isStringType("uuid")).toBe(false);
867
+ expect(XtkCaster.isStringType(12)).toBe(true);
868
+ expect(XtkCaster.isStringType(13)).toBe(true);
869
+ expect(XtkCaster.isStringType("memo")).toBe(true);
870
+ expect(XtkCaster.isStringType("CDATA")).toBe(true);
871
+ expect(XtkCaster.isStringType("blob")).toBe(true);
872
+ expect(XtkCaster.isStringType("html")).toBe(true);
873
+ expect(XtkCaster.isStringType(1)).toBe(false);
874
+ expect(XtkCaster.isStringType(2)).toBe(false);
875
+ expect(XtkCaster.isStringType(3)).toBe(false);
876
+ expect(XtkCaster.isStringType(15)).toBe(false);
877
+ expect(XtkCaster.isStringType("byte")).toBe(false);
878
+ expect(XtkCaster.isStringType("short")).toBe(false);
879
+ expect(XtkCaster.isStringType("long")).toBe(false);
880
+ expect(XtkCaster.isStringType("int")).toBe(false);
881
+ expect(XtkCaster.isStringType("boolean")).toBe(false);
882
+ expect(XtkCaster.isStringType(4)).toBe(false);
883
+ expect(XtkCaster.isStringType(5)).toBe(false);
884
+ expect(XtkCaster.isStringType("float")).toBe(false);
885
+ expect(XtkCaster.isStringType("double")).toBe(false);
886
+ expect(XtkCaster.isStringType(7)).toBe(false);
887
+ expect(XtkCaster.isStringType(10)).toBe(false);
888
+ expect(XtkCaster.isStringType("datetime")).toBe(false);
889
+ expect(XtkCaster.isStringType("timestamp")).toBe(false);
890
+ expect(XtkCaster.isStringType("datetimetz")).toBe(false);
891
+ expect(XtkCaster.isStringType("datetimenotz")).toBe(false);
892
+ expect(XtkCaster.isStringType("date")).toBe(false);
893
+ expect(XtkCaster.isStringType(14)).toBe(false);
894
+ expect(XtkCaster.isStringType("time")).toBe(false);
895
+ expect(XtkCaster.isStringType("timespan")).toBe(false);
896
+ });
897
+
898
+ it("Should check number type", () => {
899
+ expect(XtkCaster.isNumericType(null)).toBe(false);
900
+ expect(XtkCaster.isNumericType(undefined)).toBe(false);
901
+ expect(XtkCaster.isNumericType(0)).toBe(false);
902
+ expect(XtkCaster.isNumericType("")).toBe(false);
903
+ expect(XtkCaster.isNumericType(6)).toBe(false);
904
+ expect(XtkCaster.isNumericType("string")).toBe(false);
905
+ expect(XtkCaster.isNumericType("int64")).toBe(false);
906
+ expect(XtkCaster.isNumericType("uuid")).toBe(false);
907
+ expect(XtkCaster.isNumericType(12)).toBe(false);
908
+ expect(XtkCaster.isNumericType(13)).toBe(false);
909
+ expect(XtkCaster.isNumericType("memo")).toBe(false);
910
+ expect(XtkCaster.isNumericType("CDATA")).toBe(false);
911
+ expect(XtkCaster.isNumericType("blob")).toBe(false);
912
+ expect(XtkCaster.isNumericType("html")).toBe(false);
913
+ expect(XtkCaster.isNumericType(1)).toBe(true);
914
+ expect(XtkCaster.isNumericType(2)).toBe(true);
915
+ expect(XtkCaster.isNumericType(3)).toBe(true);
916
+ expect(XtkCaster.isNumericType(15)).toBe(false);
917
+ expect(XtkCaster.isNumericType("byte")).toBe(true);
918
+ expect(XtkCaster.isNumericType("short")).toBe(true);
919
+ expect(XtkCaster.isNumericType("long")).toBe(true);
920
+ expect(XtkCaster.isNumericType("int")).toBe(true);
921
+ expect(XtkCaster.isNumericType("boolean")).toBe(false);
922
+ expect(XtkCaster.isNumericType(4)).toBe(true);
923
+ expect(XtkCaster.isNumericType(5)).toBe(true);
924
+ expect(XtkCaster.isNumericType("float")).toBe(true);
925
+ expect(XtkCaster.isNumericType("double")).toBe(true);
926
+ expect(XtkCaster.isNumericType(7)).toBe(false);
927
+ expect(XtkCaster.isNumericType(10)).toBe(false);
928
+ expect(XtkCaster.isNumericType("datetime")).toBe(false);
929
+ expect(XtkCaster.isNumericType("timestamp")).toBe(false);
930
+ expect(XtkCaster.isNumericType("datetimetz")).toBe(false);
931
+ expect(XtkCaster.isNumericType("datetimenotz")).toBe(false);
932
+ expect(XtkCaster.isNumericType("date")).toBe(false);
933
+ expect(XtkCaster.isNumericType(14)).toBe(true);
934
+ expect(XtkCaster.isNumericType("time")).toBe(false);
935
+ expect(XtkCaster.isNumericType("timespan")).toBe(true);
936
+ });
816
937
  });