Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: objective-c
osx_image: xcode7.2

before_install:
- cd Example
Expand Down
59 changes: 31 additions & 28 deletions Classes/NSArray+ObjectiveSugar.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,33 @@

#import <Foundation/Foundation.h>

@interface NSArray (ObjectiveSugar)
NS_ASSUME_NONNULL_BEGIN

@interface NSArray<__covariant ObjectType> (ObjectiveSugar)

/**
The first item in the array, or nil.

@return The first item in the array, or nil.
*/
- (id)first DEPRECATED_MSG_ATTRIBUTE("Please use -firstObject instead");
- (nullable ObjectType)first DEPRECATED_MSG_ATTRIBUTE("Please use -firstObject instead");

/**
The last item in the array, or nil.

@return The last item in the array, or nil.
*/
- (id)last DEPRECATED_MSG_ATTRIBUTE("Please use -lastObject instead");
- (nullable ObjectType)last DEPRECATED_MSG_ATTRIBUTE("Please use -lastObject instead");

/**
A random element in the array, or nil.

@return A random element in the array, or nil.
*/
- (id)sample;
- (nullable ObjectType)sample;

/// Alias for -sample
- (id)anyObject;
- (nullable ObjectType)anyObject;


/**
Expand All @@ -50,22 +52,22 @@

@return An array with elements within the specified range
*/
- (id)objectForKeyedSubscript:(id <NSCopying>)key;
- (NSArray<ObjectType> *)objectForKeyedSubscript:(id <NSCopying>)key;


/**
A simpler alias for `enumerateObjectsUsingBlock`

@param block A block with the object in its arguments.
*/
- (void)each:(void (^)(id object))block;
- (void)each:(void (^)(ObjectType object))block;

/**
A simpler alias for `enumerateObjectsUsingBlock` which also passes in an index

@param block A block with the object in its arguments.
*/
- (void)eachWithIndex:(void (^)(id object, NSUInteger index))block;
- (void)eachWithIndex:(void (^)(ObjectType object, NSUInteger index))block;

/**
A simpler alias for `enumerateObjectsWithOptions:usingBlock:`
Expand All @@ -74,7 +76,7 @@
@param options Enumerating options.
*/

- (void)each:(void (^)(id object))block options:(NSEnumerationOptions)options;
- (void)each:(void (^)(ObjectType object))block options:(NSEnumerationOptions)options;

/**
A simpler alias for `enumerateObjectsWithOptions:usingBlock:` which also passes in an index
Expand All @@ -83,15 +85,15 @@
@param options Enumerating options.
*/

- (void)eachWithIndex:(void (^)(id object, NSUInteger index))block options:(NSEnumerationOptions)options;
- (void)eachWithIndex:(void (^)(ObjectType object, NSUInteger index))block options:(NSEnumerationOptions)options;


/**
An alias for `containsObject`

@param object An object that the array may or may not contain.
*/
- (BOOL)includes:(id)object;
- (BOOL)includes:(ObjectType)object;

/**
Take the first `numberOfElements` out of the array, or the maximum amount of
Expand All @@ -100,7 +102,7 @@
@param numberOfElements Number of elements to take from array
@return An array of elements
*/
- (NSArray *)take:(NSUInteger)numberOfElements;
- (NSArray<ObjectType> *)take:(NSUInteger)numberOfElements;

/**
Passes elements to the `block` until the block returns NO,
Expand All @@ -109,7 +111,7 @@
@param block A block that returns YES/NO
@return An array of elements
*/
- (NSArray *)takeWhile:(BOOL (^)(id object))block;
- (NSArray<ObjectType> *)takeWhile:(BOOL (^)(ObjectType object))block;

/**
Iterate through the current array running the block on each object and
Expand All @@ -118,23 +120,23 @@
@param block A block that passes in each object and returns a modified object
@return An array of modified elements
*/
- (NSArray *)map:(id (^)(id object))block;
- (NSArray *)map:(id(^)(ObjectType object))block;

/**
Iterate through current array asking whether to keep each element.

@param block A block that returns YES/NO for whether the object should stay
@return An array of elements selected
*/
- (NSArray *)select:(BOOL (^)(id object))block;
- (NSArray<ObjectType> *)select:(BOOL (^)(ObjectType object))block;

/**
Iterate through current array returning the first element meeting a criteria.

@param block A block that returns YES/NO
@return The first matching element
*/
- (id)detect:(BOOL (^)(id object))block;
- (nullable ObjectType)detect:(BOOL (^)(ObjectType object))block;


/**
Expand All @@ -144,15 +146,15 @@
@param block A block that returns YES/NO
@return The first matching element
*/
- (id)find:(BOOL (^)(id object))block;
- (nullable ObjectType)find:(BOOL (^)(ObjectType object))block;

/**
Iterate through current array asking whether to remove each element.

@param block A block that returns YES/NO for whether the object should be removed
@return An array of elements not rejected
*/
- (NSArray *)reject:(BOOL (^)(id object))block;
- (NSArray<ObjectType> *)reject:(BOOL (^)(ObjectType object))block;

/**
Recurse through self checking for NSArrays and extract all elements into one single array
Expand Down Expand Up @@ -187,29 +189,29 @@

@return A sorted copy of the array
*/
- (NSArray *)sort;
- (NSArray<ObjectType> *)sort;

/**
Sorts the array using the the default comparator on the given key

@return A sorted copy of the array
*/
- (NSArray *)sortBy:(NSString *)key;
- (NSArray<ObjectType> *)sortBy:(NSString *)key;

/**
Alias for reverseObjectEnumerator.allObjects

Returns a reversed array
*/
- (NSArray *)reverse;
- (NSArray<ObjectType> *)reverse;

/**
Return all the objects that are in both self and `array`.
Alias for Ruby's & operator

@return An array of objects common to both arrays
*/
- (NSArray *)intersectionWithArray:(NSArray *)array;
- (NSArray<ObjectType> *)intersectionWithArray:(NSArray<ObjectType> *)array;

/**
Return all the objects that in both self and `array` combined.
Expand All @@ -218,7 +220,7 @@
@return An array of the two arrays combined
*/

- (NSArray *)unionWithArray:(NSArray *)array;
- (NSArray<ObjectType> *)unionWithArray:(NSArray<ObjectType> *)array;

/**
Return all the objects in self that are not in `array`.
Expand All @@ -227,34 +229,35 @@
@return An array of the self without objects in `array`
*/

- (NSArray *)relativeComplement:(NSArray *)array;
- (NSArray<ObjectType> *)relativeComplement:(NSArray<ObjectType> *)array;

/**
Return all the objects that are unique to each array individually
Alias for Ruby's ^ operator. Equivalent of a - b | b - a

@return An array of elements which are in either of the arrays and not in their intersection.
*/
- (NSArray *)symmetricDifference:(NSArray *)array;
- (NSArray<ObjectType> *)symmetricDifference:(NSArray<ObjectType> *)array;

/**
Return a single value from an array by iterating through the elements and transforming a running total.

@return A single value that is the end result of apply the block function to each element successively.
**/
- (id)reduce:(id (^)(id accumulator, id object))block;
- (nullable id)reduce:(id __nullable (^)(id __nullable accumulator, ObjectType object))block;

/**
Same as -reduce, with initial value provided by yourself
**/
- (id)reduce:(id)initial withBlock:(id (^)(id accumulator, id object))block;
- (nullable id)reduce:(nullable id)initial withBlock:(id __nullable (^)(id __nullable accumulator, ObjectType object))block;

/**
Produces a duplicate-free version of the array

@return a new array with all unique elements
**/
- (NSArray *)unique;
- (NSArray<ObjectType> *)unique;

@end

NS_ASSUME_NONNULL_END
26 changes: 15 additions & 11 deletions Classes/NSDictionary+ObjectiveSugar.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@

#import <Foundation/Foundation.h>

@interface NSDictionary (ObjectiveSugar)
NS_ASSUME_NONNULL_BEGIN

- (void)each:(void (^)(id key, id value))block;
- (void)eachKey:(void (^)(id key))block;
- (void)eachValue:(void (^)(id value))block;
- (NSArray *)map:(id (^)(id key, id value))block;
- (BOOL)hasKey:(id)key;
- (NSDictionary *)pick:(NSArray *)keys;
- (NSDictionary *)omit:(NSArray *)keys;
- (NSDictionary *)merge:(NSDictionary *)dictionary;
- (NSDictionary *)merge:(NSDictionary *)dictionary block:(id(^)(id key, id oldVal, id newVal))block;
- (NSDictionary *)invert;
@interface NSDictionary<__covariant KeyType, __covariant ObjectType> (ObjectiveSugar)

- (void)each:(void (^)(KeyType key, ObjectType value))block;
- (void)eachKey:(void (^)(KeyType key))block;
- (void)eachValue:(void (^)(ObjectType value))block;
- (NSArray *)map:(id __nullable (^)(KeyType key, ObjectType value))block;
- (BOOL)hasKey:(KeyType)key;
- (NSDictionary<KeyType, ObjectType> *)pick:(NSArray<KeyType> *)keys;
- (NSDictionary<KeyType, ObjectType> *)omit:(NSArray<KeyType> *)keys;
- (NSDictionary<KeyType, ObjectType> *)merge:(NSDictionary<KeyType, ObjectType> *)dictionary;
- (NSDictionary *)merge:(NSDictionary *)dictionary block:(id __nullable (^)(id key, ObjectType oldVal, id newVal))block;
- (NSDictionary<ObjectType, KeyType> *)invert;

@end

NS_ASSUME_NONNULL_END
20 changes: 12 additions & 8 deletions Classes/NSMutableArray+ObjectiveSugar.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@

#import <Foundation/Foundation.h>

@interface NSMutableArray (ObjectiveSugar)
NS_ASSUME_NONNULL_BEGIN

@interface NSMutableArray<ObjectType> (ObjectiveSugar)

/// Alias for -addObject. Appends the given object at the end
- (void)push:(id)object;
- (void)push:(ObjectType)object;

/**
Removes the last item of the array, and returns that item
Note: This method changes the length of the array!

@return First array item or nil.
*/
- (id)pop;
- (nullable ObjectType)pop;


/**
Expand All @@ -28,8 +30,8 @@

@return First array item or nil.
*/
- (NSArray *)pop:(NSUInteger)numberOfElements;
- (void)concat:(NSArray *)array;
- (NSArray<ObjectType> *)pop:(NSUInteger)numberOfElements;
- (void)concat:(NSArray<ObjectType> *)array;


/**
Expand All @@ -38,7 +40,7 @@

@return First array item or nil.
*/
- (id)shift;
- (nullable ObjectType)shift;


/**
Expand All @@ -47,7 +49,7 @@

@return Array of first N items or empty array.
*/
- (NSArray *)shift:(NSUInteger)numberOfElements;
- (NSArray<ObjectType> *)shift:(NSUInteger)numberOfElements;


/**
Expand All @@ -56,6 +58,8 @@
@param block block that returns YES/NO
@return An array of elements
*/
- (NSArray *)keepIf:(BOOL (^)(id object))block;
- (NSArray<ObjectType> *)keepIf:(BOOL (^)(ObjectType object))block;

@end

NS_ASSUME_NONNULL_END
4 changes: 4 additions & 0 deletions Classes/NSNumber+ObjectiveSugar.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSNumber (ObjectiveSugar)

- (void)times:(void(^)(void))block;
Expand Down Expand Up @@ -43,3 +45,5 @@
- (NSDate *)fromNow;

@end

NS_ASSUME_NONNULL_END
Loading