-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSArray+Convenience.m
273 lines (206 loc) · 6.64 KB
/
NSArray+Convenience.m
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//
// NSArray+Convenience.m
// Sleep Diary
//
// Created by Alexander Ivanov on 16.10.16.
// Copyright © 2016 Alexander Ivanov. All rights reserved.
//
#import "NSArray+Convenience.h"
@implementation NSArray (Convenience)
+ (instancetype)arrayWithObject:(id)anObject count:(NSUInteger)count {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:count];
for (NSUInteger index = 0; index < count; index++)
[array addObject:anObject];
return array;
}
+ (instancetype)arrayWithObject:(id)object1 withObject:(id)object2 {
return [self arrayWithObject:object1 withObject:object2 withObject:Nil];
}
+ (instancetype)arrayWithObject:(id)object1 withObject:(id)object2 withObject:(id)object3 {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:3];
if (object1)
[array addObject:object1];
if (object2)
[array addObject:object2];
if (object3)
[array addObject:object3];
return array;
}
- (NSArray *)arrayWithRange:(NSRange)range {
return range.location < self.count ? [self subarrayWithRange:NSMakeRange(range.location, (range.location + range.length) < self.count ? range.length : self.count - range.location)] : Nil;
}
- (NSArray *)arrayWithCount:(NSUInteger)count {
return [self arrayWithRange:NSMakeRange(0, count > self.count ? self.count : count)];
}
- (NSArray *)arrayWithIndex:(NSUInteger)index {
return index < self.count ? [self arrayWithRange:NSMakeRange(index, self.count - index)] : Nil;
}
- (NSArray *)arrayByAddingObjectsFromNullableArray:(NSArray *)otherArray {
return otherArray.count ? [self arrayByAddingObjectsFromArray:otherArray] : self;
}
- (NSArray *)arrayByAddingNullableObject:(id)anObject {
return anObject ? [self arrayByAddingObject:anObject] : self;
}
- (NSArray *)arrayByRemovingObjectsFromArray:(NSArray *)otherArray {
if (!otherArray)
return self;
NSMutableArray *array = [self mutableCopy];
for (id obj in otherArray)
[array removeObject:obj];
return array;
}
- (NSArray *)arrayByRemovingObject:(id)anObject {
if (!anObject)
return self;
NSMutableArray *array = [self mutableCopy];
[array removeObject:anObject];
return array;
}
- (NSArray *)arrayByRemovingObjectAtIndex:(NSUInteger)index {
if (self.count <= index)
return self;
NSMutableArray *array = [self mutableCopy];
[array removeObjectAtIndex:index];
return array;
}
- (NSArray *)reversedArray {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:self.count];
for (NSInteger index = self.count - 1; index >= 0; index--)
[array addObject:self[index]];
return array;
}
- (NSArray *)sortedArray:(BOOL)descending {
return [self sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return descending ? [obj2 respondsToSelector:@selector(compare:)] ? [obj2 compare:obj1] : NSOrderedSame : [obj1 respondsToSelector:@selector(compare:)] ? [obj1 compare:obj2] : NSOrderedSame;
}];
}
- (NSArray *)sortedArray {
return [self sortedArray:NO];
}
- (NSArray *)map:(id (^)(id))predicate {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:self.count];
for (id obj in self) {
id object = predicate ? predicate(obj) : obj;
if (object)
[array addObject:object];
}
return array;
}
- (NSArray *)query:(BOOL (^)(id))predicate {
return [self map:predicate ? ^id(id obj) {
return predicate(obj) ? obj : Nil;
} : Nil];
}
- (NSUInteger)first:(BOOL (^)(id))predicate {
NSInteger count = self.count;
for (NSInteger index = 0; index < count; index++)
if (!predicate || predicate(self[index]))
return index;
return NSNotFound;
}
- (id)firstObject:(BOOL (^)(id))predicate {
NSUInteger index = [self first:predicate];
return index == NSNotFound ? Nil : self[index];
}
- (NSUInteger)last:(BOOL (^)(id))predicate {
NSInteger count = self.count;
for (NSInteger index = count - 1; index >= 0; index--)
if (!predicate || predicate(self[index]))
return index;
return NSNotFound;
}
- (id)lastObject:(BOOL (^)(id))predicate {
NSUInteger index = [self last:predicate];
return index == NSNotFound ? Nil : self[index];
}
- (BOOL)all:(BOOL (^)(id))predicate {
for (id item in self)
if (predicate && !predicate(item))
return NO;
return YES;
}
- (BOOL)any:(BOOL (^)(id))predicate {
for (id item in self)
if (!predicate || predicate(item))
return YES;
return NO;
}
- (id)one:(BOOL (^)(id, id))predicate {
id one = Nil;
for (id obj in self)
if (predicate && predicate(one, obj))
one = obj;
return one;
}
- (NSDictionary *)dictionaryWithKey:(id<NSCopying>(^)(id))keyPredicate value:(id(^)(id, id<NSCopying>, id))valPredicate {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:self.count];
for (id obj in self) {
id<NSCopying> key = keyPredicate ? keyPredicate(obj) : obj;
if (key) {
id val = dictionary[key];
val = valPredicate ? valPredicate(obj, key, val) : obj;
dictionary[key] = val;
}
}
return dictionary;
}
- (NSDictionary *)dictionaryWithKey:(id<NSCopying>(^)(id))keyPredicate {
return [self dictionaryWithKey:keyPredicate value:Nil];
}
+ (instancetype)arrayFromRange:(NSRange)range block:(id (^)(NSUInteger))block {
NSMutableArray *array = [NSMutableArray arrayWithCapacity:range.length];
NSUInteger count = range.location + range.length;
for (NSUInteger index = range.location; index < count; index++) {
id object = block ? block(index) : @(index);
if (object)
[array addObject:object];
}
return array;
}
+ (instancetype)arrayFromCount:(NSUInteger)count block:(id (^)(NSUInteger))block {
return [self arrayFromRange:NSMakeRange(0, count) block:block];
}
- (NSString *)componentsJoinedByString:(NSString *)separator block:(NSString *(^)(id))block {
NSMutableString *description = [[NSMutableString alloc] init];
for (NSUInteger index = 0; index < self.count; index++) {
if (index > 0 && separator.length)
[description appendString:separator];
NSString *string = block ? block(self[index]) : [self[index] description];
if (string)
[description appendString:string];
}
return description;
}
- (BOOL)isEqualToArray:(NSArray *)otherArray block:(BOOL(^)(id, id))predicate {
if (self.count != otherArray.count)
return NO;
for (NSUInteger index = 0; index < self.count; index++)
if (!predicate(self[index], otherArray[index]))
return NO;
return YES;
}
- (id)randomObject {
if (self.count == 0)
return Nil;
if (self.count == 1)
return [self objectAtIndex:0];
NSUInteger index = arc4random() % self.count;
return [self objectAtIndex:index];
}
@end
@implementation NSMutableArray (Convenience)
- (id)fifo {
if (!self.count)
return Nil;
id obj = self.firstObject;
[self removeObjectAtIndex:0];
return obj;
}
- (id)lifo {
if (!self.count)
return Nil;
id obj = self.lastObject;
[self removeObjectAtIndex:self.count - 1];
return obj;
}
@end