0
# Object Operations
1
2
Object manipulation utilities for converting between objects and arrays, iterating over properties, and transforming object structures. The Object module provides 14 functions for comprehensive object handling.
3
4
## Capabilities
5
6
### Object Conversion
7
8
Functions for converting between objects and various array representations.
9
10
```javascript { .api }
11
/**
12
* Gets array of object values
13
* @param {Object} object - Object to extract values from
14
* @returns {Array} Array of object values
15
*/
16
function values(object);
17
18
/**
19
* Gets array of object keys
20
* @param {Object} object - Object to extract keys from
21
* @returns {Array<string>} Array of object keys
22
*/
23
function keys(object);
24
25
/**
26
* Converts array of [key, value] pairs to object
27
* @param {Array<Array>} pairs - Array of [key, value] pairs
28
* @returns {Object} Object created from pairs
29
*/
30
function pairsToObj(pairs);
31
32
/**
33
* Converts object to array of [key, value] pairs
34
* @param {Object} object - Object to convert
35
* @returns {Array<Array>} Array of [key, value] pairs
36
*/
37
function objToPairs(object);
38
39
/**
40
* Creates object from separate keys and values arrays
41
* @param {Array<string>} keys - Array of keys
42
* @param {Array} values - Array of values
43
* @returns {Object} Object with paired keys and values
44
*/
45
function listsToObj(keys, values);
46
47
/**
48
* Converts object to [keys, values] arrays
49
* @param {Object} object - Object to convert
50
* @returns {Array} [keysArray, valuesArray]
51
*/
52
function objToLists(object);
53
```
54
55
### Object Operations
56
57
Functions for manipulating and querying objects.
58
59
```javascript { .api }
60
/**
61
* Checks if object has no properties
62
* @param {Object} object - Object to check
63
* @returns {boolean} True if object has no enumerable properties
64
*/
65
function empty(object);
66
67
/**
68
* Applies function to each value, returns original object
69
* @param {Function} fn - Function to apply to each value
70
* @param {Object} object - Object to iterate over
71
* @returns {Object} Original object (for chaining)
72
*/
73
function each(fn, object);
74
75
/**
76
* Transforms each value with function
77
* @param {Function} fn - Transformation function
78
* @param {Object} object - Object to transform
79
* @returns {Object} New object with transformed values
80
*/
81
function map(fn, object);
82
83
/**
84
* Removes properties with falsy values
85
* @param {Object} object - Object to compact
86
* @returns {Object} Object without falsy-valued properties
87
*/
88
function compact(object);
89
90
/**
91
* Keeps properties where value matches predicate
92
* @param {Function} predicate - Function returning boolean
93
* @param {Object} object - Object to filter
94
* @returns {Object} Object with only matching properties
95
*/
96
function filter(predicate, object);
97
98
/**
99
* Removes properties where value matches predicate
100
* @param {Function} predicate - Function returning boolean
101
* @param {Object} object - Object to filter
102
* @returns {Object} Object with non-matching properties removed
103
*/
104
function reject(predicate, object);
105
106
/**
107
* Splits object into [passed, failed] based on predicate
108
* @param {Function} predicate - Function returning boolean
109
* @param {Object} object - Object to partition
110
* @returns {Array<Object>} [passedObject, failedObject]
111
*/
112
function partition(predicate, object);
113
114
/**
115
* Finds first value matching predicate
116
* @param {Function} predicate - Function returning boolean
117
* @param {Object} object - Object to search
118
* @returns {*} First matching value or undefined
119
*/
120
function find(predicate, object);
121
```
122
123
## Usage Examples
124
125
**Object-Array Conversion:**
126
127
```javascript
128
const { keys, values, objToPairs, pairsToObj } = require('prelude-ls');
129
130
const user = { name: 'Alice', age: 30, city: 'Boston' };
131
132
const userKeys = keys(user); // ['name', 'age', 'city']
133
const userValues = values(user); // ['Alice', 30, 'Boston']
134
const userPairs = objToPairs(user); // [['name', 'Alice'], ['age', 30], ['city', 'Boston']]
135
136
// Reconstruct object
137
const reconstructed = pairsToObj(userPairs); // { name: 'Alice', age: 30, city: 'Boston' }
138
```
139
140
**Object Transformation:**
141
142
```javascript
143
const { map, filter, keys } = require('prelude-ls');
144
145
const scores = { math: 85, english: 92, science: 78 };
146
147
// Transform values
148
const curved = map(score => score + 5, scores);
149
// Result: { math: 90, english: 97, science: 83 }
150
151
// Filter by value
152
const highScores = filter(score => score > 90, curved);
153
// Result: { english: 97 }
154
155
// Get subject names with high scores
156
const highSubjects = keys(highScores); // ['english']
157
```
158
159
**Object Property Operations:**
160
161
```javascript
162
const { compact, partition, find } = require('prelude-ls');
163
164
const data = {
165
valid: 'data',
166
empty: '',
167
zero: 0,
168
text: 'hello',
169
nullValue: null
170
};
171
172
// Remove falsy values
173
const cleaned = compact(data);
174
// Result: { valid: 'data', text: 'hello' }
175
176
// Partition by string values
177
const [strings, nonStrings] = partition(
178
value => typeof value === 'string',
179
data
180
);
181
// strings: { valid: 'data', empty: '', text: 'hello' }
182
// nonStrings: { zero: 0, nullValue: null }
183
184
// Find first string value
185
const firstString = find(value => typeof value === 'string', data);
186
// Result: 'data'
187
```
188
189
**Lists and Objects Integration:**
190
191
```javascript
192
const { listsToObj, objToLists } = require('prelude-ls');
193
194
const subjects = ['math', 'english', 'science'];
195
const grades = ['A', 'B', 'A'];
196
197
// Create object from parallel arrays
198
const gradebook = listsToObj(subjects, grades);
199
// Result: { math: 'A', english: 'B', science: 'A' }
200
201
// Extract back to parallel arrays
202
const [subjectList, gradeList] = objToLists(gradebook);
203
// subjectList: ['math', 'english', 'science']
204
// gradeList: ['A', 'B', 'A']
205
```
206
207
**Curried Object Processing:**
208
209
```javascript
210
const { map, filter } = require('prelude-ls');
211
212
// Create reusable object processors
213
const incrementValues = map(x => x + 1);
214
const keepPositive = filter(x => x > 0);
215
216
const numbers = { a: 1, b: -2, c: 3, d: 0 };
217
218
const processed = keepPositive(incrementValues(numbers));
219
// Result: { a: 2, c: 4 }
220
```
221
222
**Object Inspection:**
223
224
```javascript
225
const { empty, keys, values } = require('prelude-ls');
226
227
const user = { name: 'Alice', preferences: {} };
228
229
const isEmpty = empty(user.preferences); // true
230
const hasData = !empty(user); // true
231
232
const fieldCount = keys(user).length; // 2
233
const hasName = values(user).includes('Alice'); // true
234
```