Skip to content

Commit 72a74d8

Browse files
authored
feat: implement objectRemoveKey (#1070)
1 parent 8ea27ab commit 72a74d8

File tree

3 files changed

+111
-89
lines changed

3 files changed

+111
-89
lines changed

doc/_stdlib_gen/stdlib-content.jsonnet

Lines changed: 101 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -70,83 +70,6 @@ local html = import 'html.libsonnet';
7070
<code>null</code>, <code>true</code> or <code>false</code>.
7171
|||,
7272
},
73-
{
74-
name: 'get',
75-
params: ['o', 'f', 'default=null', 'inc_hidden=true'],
76-
availableSince: '0.18.0',
77-
description: |||
78-
Returns the object's field if it exists or default value otherwise.
79-
<code>inc_hidden</code> controls whether to include hidden fields.
80-
|||,
81-
},
82-
{
83-
name: 'objectHas',
84-
params: ['o', 'f'],
85-
availableSince: '0.10.0',
86-
description: |||
87-
Returns <code>true</code> if the given object has the field (given as a string), otherwise
88-
<code>false</code>. Raises an error if the arguments are not object and string
89-
respectively. Returns false if the field is hidden.
90-
|||,
91-
},
92-
{
93-
name: 'objectFields',
94-
params: ['o'],
95-
availableSince: '0.10.0',
96-
description: |||
97-
Returns an array of strings, each element being a field from the given object. Does not include
98-
hidden fields.
99-
|||,
100-
},
101-
{
102-
name: 'objectValues',
103-
params: ['o'],
104-
availableSince: '0.17.0',
105-
description: |||
106-
Returns an array of the values in the given object. Does not include hidden fields.
107-
|||,
108-
},
109-
{
110-
name: 'objectKeysValues',
111-
params: ['o'],
112-
availableSince: '0.20.0',
113-
description: |||
114-
Returns an array of objects from the given object, each object having two fields:
115-
<code>key</code> (string) and <code>value</code> (object). Does not include hidden fields.
116-
|||,
117-
},
118-
{
119-
name: 'objectHasAll',
120-
params: ['o', 'f'],
121-
availableSince: '0.10.0',
122-
description: |||
123-
As <code>std.objectHas</code> but also includes hidden fields.
124-
|||,
125-
},
126-
{
127-
name: 'objectFieldsAll',
128-
params: ['o'],
129-
availableSince: '0.10.0',
130-
description: |||
131-
As <code>std.objectFields</code> but also includes hidden fields.
132-
|||,
133-
},
134-
{
135-
name: 'objectValuesAll',
136-
params: ['o'],
137-
availableSince: '0.17.0',
138-
description: |||
139-
As <code>std.objectValues</code> but also includes hidden fields.
140-
|||,
141-
},
142-
{
143-
name: 'objectKeysValuesAll',
144-
params: ['o'],
145-
availableSince: '0.20.0',
146-
description: |||
147-
As <code>std.objectKeysValues</code> but also includes hidden fields.
148-
|||,
149-
},
15073
{
15174
name: 'prune',
15275
params: ['a'],
@@ -157,16 +80,6 @@ local html = import 'html.libsonnet';
15780
The argument <code>a</code> may have any type.
15881
|||,
15982
},
160-
{
161-
name: 'mapWithKey',
162-
params: ['func', 'obj'],
163-
availableSince: '0.10.0',
164-
description: |||
165-
Apply the given function to all fields of the given object, also passing
166-
the field name. The function <code>func</code> is expected to take the
167-
field name as the first parameter and the field value as the second.
168-
|||,
169-
},
17083
],
17184
},
17285
{
@@ -1515,6 +1428,107 @@ local html = import 'html.libsonnet';
15151428
},
15161429
],
15171430
},
1431+
{
1432+
name: 'Objects',
1433+
id: 'objects',
1434+
fields: [
1435+
{
1436+
name: 'get',
1437+
params: ['o', 'f', 'default=null', 'inc_hidden=true'],
1438+
availableSince: '0.18.0',
1439+
description: |||
1440+
Returns the object's field if it exists or default value otherwise.
1441+
<code>inc_hidden</code> controls whether to include hidden fields.
1442+
|||,
1443+
},
1444+
{
1445+
name: 'objectHas',
1446+
params: ['o', 'f'],
1447+
availableSince: '0.10.0',
1448+
description: |||
1449+
Returns <code>true</code> if the given object has the field (given as a string), otherwise
1450+
<code>false</code>. Raises an error if the arguments are not object and string
1451+
respectively. Returns false if the field is hidden.
1452+
|||,
1453+
},
1454+
{
1455+
name: 'objectFields',
1456+
params: ['o'],
1457+
availableSince: '0.10.0',
1458+
description: |||
1459+
Returns an array of strings, each element being a field from the given object. Does not include
1460+
hidden fields.
1461+
|||,
1462+
},
1463+
{
1464+
name: 'objectValues',
1465+
params: ['o'],
1466+
availableSince: '0.17.0',
1467+
description: |||
1468+
Returns an array of the values in the given object. Does not include hidden fields.
1469+
|||,
1470+
},
1471+
{
1472+
name: 'objectKeysValues',
1473+
params: ['o'],
1474+
availableSince: '0.20.0',
1475+
description: |||
1476+
Returns an array of objects from the given object, each object having two fields:
1477+
<code>key</code> (string) and <code>value</code> (object). Does not include hidden fields.
1478+
|||,
1479+
},
1480+
{
1481+
name: 'objectHasAll',
1482+
params: ['o', 'f'],
1483+
availableSince: '0.10.0',
1484+
description: |||
1485+
As <code>std.objectHas</code> but also includes hidden fields.
1486+
|||,
1487+
},
1488+
{
1489+
name: 'objectFieldsAll',
1490+
params: ['o'],
1491+
availableSince: '0.10.0',
1492+
description: |||
1493+
As <code>std.objectFields</code> but also includes hidden fields.
1494+
|||,
1495+
},
1496+
{
1497+
name: 'objectValuesAll',
1498+
params: ['o'],
1499+
availableSince: '0.17.0',
1500+
description: |||
1501+
As <code>std.objectValues</code> but also includes hidden fields.
1502+
|||,
1503+
},
1504+
{
1505+
name: 'objectKeysValuesAll',
1506+
params: ['o'],
1507+
availableSince: '0.20.0',
1508+
description: |||
1509+
As <code>std.objectKeysValues</code> but also includes hidden fields.
1510+
|||,
1511+
},
1512+
{
1513+
name: 'objectRemoveKey',
1514+
params: ['obj', 'key'],
1515+
availableSince: 'upcoming',
1516+
description: |||
1517+
Returns a new object after removing the given key from object.
1518+
|||,
1519+
},
1520+
{
1521+
name: 'mapWithKey',
1522+
params: ['func', 'obj'],
1523+
availableSince: '0.10.0',
1524+
description: |||
1525+
Apply the given function to all fields of the given object, also passing
1526+
the field name. The function <code>func</code> is expected to take the
1527+
field name as the first parameter and the field value as the second.
1528+
|||,
1529+
},
1530+
],
1531+
},
15181532
{
15191533
name: 'Encoding',
15201534
id: 'encoding',

stdlib/std.jsonnet

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,4 +1724,10 @@ limitations under the License.
17241724
isEmpty(str):: std.length(str) == 0,
17251725

17261726
contains(arr, elem):: std.any([e == elem for e in arr]),
1727+
1728+
objectRemoveKey(obj, key):: {
1729+
[k]: obj[k],
1730+
for k in std.objectFields(obj)
1731+
if k != key
1732+
},
17271733
}

test_suite/stdlib.jsonnet

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,7 +1559,9 @@ std.assertEqual(std.round(1.5), 2) &&
15591559
std.assertEqual(std.isEmpty(''), true) &&
15601560
std.assertEqual(std.isEmpty('non-empty string'), false) &&
15611561

1562-
std.contains(std.contains([1, 2, 3], 2), true) &&
1563-
std.contains(std.contains([1, 2, 3], "foo"), false) &&
1562+
std.assertEqual(std.contains([1, 2, 3], 2), true) &&
1563+
std.assertEqual(std.contains([1, 2, 3], "foo"), false) &&
1564+
1565+
std.assertEqual(std.objectRemoveKey({ foo: 1, bar: 2, baz: 3 }, 'foo'), { bar: 2, baz: 3 }) &&
15641566

15651567
true

0 commit comments

Comments
 (0)