summaryrefslogtreecommitdiffhomepage
path: root/tests/custom/03_stdlib/50_uniq
blob: 6e130773da533f98269ce4792a9b55a486da38f2 (plain)
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
The `uniq()` function extracts the unique set of all values within the
given input array, maintaining the original order.

Returns an array containing all unique items of the input array.

Returns `null` if the input is not an array value.

-- Testcase --
{%
	let o1 = { an: "object" };
	let o2 = { an: "object" }; // same but not identical

	let a1 = [ 1, 2, 3 ];
	let a2 = [ 1, 2, 3 ]; // same but not identical

	printf("%.J\n", [
		// strict comparison is used, 0 and "0" are not unique
		uniq([ 0, 1, 2, 0, "0", 2, 3, "4", 4 ]),

		// despite NaN != NaN, two NaN values are not unique
		uniq([ NaN, NaN ]),

		// only identical objects are filtered, not equivalent ones
		uniq([ o1, o1, o2, a1, a1, a2 ]),

		// invalid input yields `null`
		uniq(true)
	]);
%}
-- End --

-- Expect stdout --
[
	[
		0,
		1,
		2,
		"0",
		3,
		"4",
		4
	],
	[
		"NaN"
	],
	[
		{
			"an": "object"
		},
		{
			"an": "object"
		},
		[
			1,
			2,
			3
		],
		[
			1,
			2,
			3
		]
	],
	null
]
-- End --