Skip to content
Browse files

collection: size

  • Loading branch information...
1 parent 6727de1 commit 1e144df62768a927c71d981aee3700e0a71b47b3 @petejkim committed
View
2 README.md
@@ -36,7 +36,7 @@ Underscore.cocoa is a utility-belt library for Objective-C & Cocoa that provides
× sortedIndex
× shuffle
× toArray
-× size
+ size
### Arrays
View
49 spec/CollectionsSpec.m
@@ -172,7 +172,7 @@
expect(arr).toEqual(_a(@"bar.", @"qux."));
});
- specify(@"object-oriented style", ^{
+ specify(@"chained", ^{
val = _(dic).chain().map(^(id value, id key, NSDictionary *list) {
expect([list objectForKey:key]).toEqual(value);
return [value stringByAppendingString:@"."];
@@ -182,4 +182,51 @@
});
});
+describe(@"size", ^{
+ context(@"Array", ^{
+ specify(@"functional style", ^{
+ expect(_.size(arr)).toEqual(2);
+ });
+
+ specify(@"object-oriented style", ^{
+ expect(_(arr).size()).toEqual(2);
+ });
+
+ specify(@"chained", ^{
+ val = _(arr).chain().size().value();
+ expect(val).toEqual([NSNumber numberWithUnsignedInteger:2]);
+ });
+ });
+
+ context(@"Set", ^{
+ specify(@"functional style", ^{
+ expect(_.size(set)).toEqual(2);
+ });
+
+ specify(@"object-oriented style", ^{
+ expect(_(set).size()).toEqual(2);
+ });
+
+ specify(@"chained", ^{
+ val = _(set).chain().size().value();
+ expect(val).toEqual([NSNumber numberWithUnsignedInteger:2]);
+ });
+ });
+
+ context(@"Dictionary", ^{
+ specify(@"functional style", ^{
+ expect(_.size(dic)).toEqual(2);
+ });
+
+ specify(@"object-oriented style", ^{
+ expect(_(dic).size()).toEqual(2);
+ });
+
+ specify(@"chained", ^{
+ val = _(dic).chain().size().value();
+ expect(val).toEqual([NSNumber numberWithUnsignedInteger:2]);
+ });
+ });
+});
+
SpecEnd
View
1 src/Underscore.h
@@ -13,6 +13,7 @@
@property (nonatomic, readonly) Underscore *(^each)(id iterator);
@property (nonatomic, readonly) Underscore *(^map)(id iterator);
+@property (nonatomic, readonly) Underscore *(^size)(void);
#pragma mark - Arrays
View
19 src/Underscore.m
@@ -37,7 +37,7 @@ + (Underscore *)underscoreWithObject:(id)object {
#pragma mark - Collections
- (Underscore *(^)(id))each {
- __block id list = self.object;
+ id list = self.object;
id block = ^Underscore *(id iterator) {
if(list && iterator) {
if([list isKindOfClass:[NSArray class]]) {
@@ -63,7 +63,7 @@ + (Underscore *)underscoreWithObject:(id)object {
}
- (Underscore *(^)(id))map {
- __block id list = self.object;
+ id list = self.object;
id block = ^Underscore *(id iterator) {
if(list && iterator) {
NSMutableArray *resultArray = [NSMutableArray arrayWithCapacity:[list count]];
@@ -89,10 +89,23 @@ + (Underscore *)underscoreWithObject:(id)object {
return [[block copy] autorelease];
}
+- (Underscore *(^)(void))size {
+ id list = self.object;
+ id block = ^Underscore * {
+ if([list isKindOfClass:[NSArray class]] || [list isKindOfClass:[NSSet class]] || [list isKindOfClass:[NSDictionary class]]) {
+ self.object = [NSNumber numberWithUnsignedInteger:[list count]];
+ } else {
+ self.object = nil;
+ }
+ return self;
+ };
+ return [[block copy] autorelease];
+}
+
#pragma mark - Arrays
- (Underscore *(^)(void))first {
- id block = ^Underscore *{
+ id block = ^Underscore * {
if([self.object isKindOfClass:[NSArray class]]) {
self.object = [self.object objectAtIndex:0];
} else {
View
1 src/UnderscoreFunctional.h
@@ -10,6 +10,7 @@
@property (nonatomic, readonly) void(^each)(id list, id iterator);
@property (nonatomic, readonly) id(^map)(id list, id iterator);
+@property (nonatomic, readonly) NSUInteger(^size)(id list);
#pragma mark - Arrays
View
7 src/UnderscoreFunctional.m
@@ -27,6 +27,13 @@ + (void)initialize {
return [[block copy] autorelease];
}
+- (NSUInteger (^)(id))size {
+ id block = ^NSUInteger(id list) {
+ return _(list).size();
+ };
+ return [[block copy] autorelease];
+}
+
#pragma mark - Arrays
- (id(^)(NSArray *))first {
View
1 src/UnderscoreUnchained.h
@@ -17,6 +17,7 @@
@property (nonatomic, readonly) void(^each)(id iterator);
@property (nonatomic, readonly) NSArray *(^map)(id iterator);
+@property (nonatomic, readonly) NSUInteger(^size)(void);
#pragma mark - Arrays
View
7 src/UnderscoreUnchained.m
@@ -39,6 +39,13 @@ + (UnderscoreUnchained *)underscoreUnchainedWithObject:(id)object {
return [[block copy] autorelease];
}
+- (NSUInteger (^)(void))size {
+ id block = ^NSUInteger(void) {
+ return [(NSNumber *)(self.chain().size().value()) unsignedIntegerValue];
+ };
+ return [[block copy] autorelease];
+}
+
#pragma mark - Arrays
- (id(^)(void))first {

0 comments on commit 1e144df

Please sign in to comment.
Something went wrong with that request. Please try again.