1 | # spec/ysh-stdlib
|
2 |
|
3 | ## our_shell: ysh
|
4 |
|
5 | #### identity
|
6 | source --builtin funcs.ysh
|
7 |
|
8 | for x in (['a', 1, null, { foo: 'bar' }, [40, 2]]) {
|
9 | json write (identity(x))
|
10 | }
|
11 |
|
12 | ## STDOUT:
|
13 | "a"
|
14 | 1
|
15 | null
|
16 | {
|
17 | "foo": "bar"
|
18 | }
|
19 | [
|
20 | 40,
|
21 | 2
|
22 | ]
|
23 | ## END
|
24 |
|
25 | #### max
|
26 | source --builtin math.ysh
|
27 |
|
28 | json write (max(1, 2))
|
29 | json write (max([1, 2, 3]))
|
30 |
|
31 | try { call max([]) }
|
32 | echo status=$_status
|
33 |
|
34 | try { call max(1, 2) }
|
35 | echo status=$_status
|
36 |
|
37 | try { call max(1, 2, 3) }
|
38 | echo status=$_status
|
39 |
|
40 | try { call max() }
|
41 | echo status=$_status
|
42 |
|
43 | ## STDOUT:
|
44 | 2
|
45 | 3
|
46 | status=3
|
47 | status=0
|
48 | status=3
|
49 | status=3
|
50 | ## END
|
51 |
|
52 | #### min
|
53 | source --builtin math.ysh
|
54 |
|
55 | json write (min(2, 3))
|
56 | json write (min([1, 2, 3]))
|
57 |
|
58 | try { call min([]) }
|
59 | echo status=$_status
|
60 |
|
61 | try { call min(2, 3) }
|
62 | echo status=$_status
|
63 |
|
64 | try { call min(1, 2, 3) }
|
65 | echo status=$_status
|
66 |
|
67 | try { call min() }
|
68 | echo status=$_status
|
69 |
|
70 | ## STDOUT:
|
71 | 2
|
72 | 1
|
73 | status=3
|
74 | status=0
|
75 | status=3
|
76 | status=3
|
77 | ## END
|
78 |
|
79 | #### abs
|
80 | source --builtin math.ysh
|
81 |
|
82 | json write (abs(-1))
|
83 | json write (abs(0))
|
84 | json write (abs(1))
|
85 | json write (abs(42))
|
86 | json write (abs(-42))
|
87 |
|
88 | try { call abs(-42) }
|
89 | echo status=$_status
|
90 |
|
91 | ## STDOUT:
|
92 | 1
|
93 | 0
|
94 | 1
|
95 | 42
|
96 | 42
|
97 | status=0
|
98 | ## END
|
99 |
|
100 | #### any
|
101 | source --builtin list.ysh
|
102 |
|
103 | json write (any([]))
|
104 | json write (any([true]))
|
105 | json write (any([false]))
|
106 | json write (any([true, false]))
|
107 | json write (any([false, true]))
|
108 | json write (any([false, false]))
|
109 | json write (any([false, true, false]))
|
110 | json write (any([false, false, null, ""])) # null and "" are falsey
|
111 | json write (any(["foo"])) # "foo" is truthy
|
112 | ## STDOUT:
|
113 | false
|
114 | true
|
115 | false
|
116 | true
|
117 | true
|
118 | false
|
119 | true
|
120 | false
|
121 | true
|
122 | ## END
|
123 |
|
124 | #### all
|
125 | source --builtin list.ysh
|
126 |
|
127 | json write (all([]))
|
128 | json write (all([true]))
|
129 | json write (all([false]))
|
130 | json write (all([true, true]))
|
131 | json write (all([true, false]))
|
132 | json write (all([false, true]))
|
133 | json write (all([false, false]))
|
134 | json write (all([false, true, false]))
|
135 | json write (all(["foo"]))
|
136 | json write (all([""]))
|
137 | ## STDOUT:
|
138 | true
|
139 | true
|
140 | false
|
141 | true
|
142 | false
|
143 | false
|
144 | false
|
145 | false
|
146 | true
|
147 | false
|
148 | ## END
|
149 |
|
150 | #### sum
|
151 | source --builtin list.ysh
|
152 |
|
153 | json write (sum([]))
|
154 | json write (sum([0]))
|
155 | json write (sum([1, 2, 3]))
|
156 | ## STDOUT:
|
157 | 0
|
158 | 0
|
159 | 6
|
160 | ## END
|