Giter Site home page Giter Site logo

collection's Introduction

NSArray+Collection

Never write another loop again

This library is inspired by Laravel collection class to use its expressive syntax. Check the .h file to see the documentation as well as all available methods.

Installation

Copy the category files to your project or just

    pod 'Collection' 

Array available methods

each:block eachWithIndex:blockWithIndex map:block flatMap:block flatMap:key:block

filter:blockChecker filterWith:keyPath reject:blockChecker rejectWith:keyPath first:blockChecker first:blockChecker:default last:blockChecker last:blockChecker:default contains:blockChecker doesntContain:blockChecker where:keyPath:like where:keyPath:is whereAny:keyPaths:like whereAny:keyPaths:is whereNull whereNull:keyPath whereNotNull whereNotNull:keyPath

sort sort:key sort:key:ascending sortWith:block:ascending

reverse random:quantity slice:howMany take:howMany splice:howMany pop

pluck:keyPath pluck:keyPath:keyKeyPath flatten flatten:keyPath reduce:block reduce:block:initialCarry pipe:block when:condition:block groupBy:keyPath gorupBy:keyPath:block expand:keypath

maxObject maxObject:keyPath maxObjectFor:block minObject minObject:keyPath minObjectFor:block

suffled permutations zip:other mapToAssoc:block countedSet implode:glue toString toJson

intersect:other union:other join:other diff:other minus:other distinct `distinct:keypath

range:to range:from:to

crossJoin:list cartesianProduct:arrays

sum sum:keyPath sumWith:block avg avg:keyPath max max:keyPath min min:keyPath countKeyPath:

map_:selector map_:selector:withObject each_:selector each_:selector:withObject filter_:selector filter_:selector:withObject reject_:selector reject_:selector:withObject

Dictionary available methods

fromData:data fromString:string toString except:keys only:keys

each:block filter:block filter:block map:block

String available methods

repeat:text:times isEmptyString explode:delimiter initials toNumber append:other prepend:other substr:from substr:from:length replace:text:with replaceRegexp:regexp:with split split:lenght trim trimWithNewLine trimLeft trimRight camelCase pascalCase snakeCase ucFirst withoutDiacritic urlEncode urlDecode md5 toBase64 fromHex:str toHex

endsWith:text startsWith:text contains:text matches:regexp

lpad:length:text rpad:length:text

Array Examples

Just some examples, check the .h or the tests to see them all In the .h there is the explanation of what it really does

    NSArray* array = @[@1,@3,@4,@5,@6];
    NSNumber* first = [array first:^BOOL(NSNumber* object) {
        return object.intValue > 4;
    }];
    NSLog(@"Fist: %@",first);
    NSNumber* second = [array first:^BOOL(NSNumber* object) {
        return object.intValue > 10;
    } default:@25];
    NSLog(@"second: %@",second);
    NSArray* oldHeroes = [self.heroes reject:^BOOL(Hero *object) {
        return object.age.intValue < 20;
    }];
    [self printHeroArray:oldHeroes];
    [self printHeroArray:[self.heroes map:^id(Hero* obj, NSUInteger idx) {
        obj.age = @(obj.age.intValue * 2);
        return obj;
    }]];
    [self printArray:[self.heroes pluck:@"enemy"]];
    NSNumber* totalAge = [self.heroes reduce:^id(NSNumber* carry, Hero* object) {
        return @(object.age.intValue + carry.intValue);
    } carry:@(0)];

    or

    NSNumber* totalAge2 = [self.heroes sum:@"age"];
    NSNumber* age = [self.heroes sum:@"age"];
    NSLog(@"Age again: %@",age);

    NSNumber* older = [self.heroes max:@"age"];
    NSLog(@"older: %@",older);

    NSNumber* younger = [self.heroes min:@"age"];
    NSLog(@"younger: %@",younger);

    NSNumber* average = [self.heroes avg:@"age"];
    NSLog(@"average: %@",average);
    [self printArray:[@[@1,@2,@3,@4] union:@[@4,@5,@6]]];
    [self printArray:[@[@1,@2,@3,@4] intersect:@[@4,@5,@6]]];
    [self printArray:[@[@1,@2,@3,@4] join:@[@4,@5,@6]]];
    [self printArray:[@[@1,@2,@3,@4] diff:@[@4,@5,@6]]];
    [self.heroes groupBy:@"age"];

    [self.heroes groupBy:@"age" block:^NSString *(Hero* object, NSString *key) {
        return str(@"age %@", object.age);
    }];
    NSArray* names = @["Spiderman", @"Batman", @"Robin", @"Luxor"];
    BOOL containsSpiderman = [self.names contains:^BOOL(Hero* hero) {
        return [hero.name isEqualToString:@"Spiderman"];
    }];
    
    BOOL heroes = [self.names where:@"name" like:@"man"];
    //HEroes => [@"Spidrman, @"Batman"]
    
    [@[@1,@2,@3,@4,@5,@6] slice:3];
    [@[@1,@2,@3,@4,@5,@6] slice:10];
    [@[@1,@2,@3,@4,@5,@6] slice:6];
    [@[@1,@2,@3,@4,@5,@6] take:2];
    [@[@1,@2,@3,@4,@5,@6] take:10];
    [@[@1,@2,@3,@4,@5,@6] take:-2];
    [@[@1,@2,@3,@4,@5,@6] take:-10];
    NSArray* array2 = @[@1,@2,@3,@4,@5].mutableCopy;
    NSArray* chunk = [array2 splice:2];
    [self printArray:chunk];
    [self printArray:array2];

Dictionary Examples

Just some examples, check the .h or the tests to see them all

    NSDictionary* filtered = [@{@"pass":@0, @"dontPass":@1} filter:^BOOL(id key, id object) {
        return object.floatValue == 0;
    }];
    NSDictionary* result = [@{@"toBeMapped":@"value", @"toBeMapped2":@"value2"} map:^id(id key, id object) {
        return [key append:object];
    }];

String Examples

Just some examples, check the .h or the tests to see them all

// [NSString stringWithFormat:@"a formated %@ string", value]; 
// becomes
str(@"a formated %@ string", value);
    NSArray* result         = [@"hola;que;tal" explode:@";"];
    NSArray* expectation    = @[@"hola",@"que",@"tal"];
    XCTAssertTrue([result isEqual:expectation]);
    NSString* result = [@"   trim   " trim];
    XCTAssertTrue( [result isEqualToString:@"trim"]);
    NSString* result = @"this should be camelcased".camelCase;
    XCTAssertTrue( [result isEqualToString:@"thisShouldBeCamelcased"]);

collection's People

Contributors

badchoice avatar paurevo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.