Last active
March 2, 2017 21:47
-
-
Save soxjke/b09175b252c597a2d468b9fbbe00cca3 to your computer and use it in GitHub Desktop.
RACSequence additions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSString *)stringByTrimmingWhiteSpacesForEachLine { | |
return [[self.rac_lineSequence map:^id(NSString * line) { | |
return [line stringByTrimmingWhiteSpaces]; | |
}] foldLeftWithStart:@"" reduce:^NSString *(NSString *accumulator, NSString *value) { | |
return [accumulator stringByAppendingFormat:@"%@\n", value]; | |
}]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSString+RACSequenceAdditions.m | |
// ReactiveCocoa | |
// | |
// Created by Justin Spahr-Summers on 2012-10-29. | |
// Copyright (c) 2012 GitHub. All rights reserved. | |
// | |
#import "NSString+RACSequenceAdditions.h" | |
#import "RACStringSequence.h" | |
#import "RACTokenizedStringSequence.h" | |
@implementation NSString (RACSequenceAdditions) | |
- (RACSequence *)rac_sequence { | |
return [RACStringSequence sequenceWithString:self offset:0]; | |
} | |
- (RACSequence *)rac_lineSequence { | |
return [self rac_sequenceWithTokenization:NSStringEnumerationByLines]; | |
} | |
- (RACTokenizedStringSequence *)rac_sequenceWithTokenization:(NSStringEnumerationOptions)tokenization { | |
return [RACTokenizedStringSequence sequenceWithString:self tokenization:tokenization]; | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSString+RACSequenceAdditions.h | |
// ReactiveCocoa | |
// | |
// Created by Justin Spahr-Summers on 2012-10-29. | |
// Copyright (c) 2012 GitHub. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@class RACSequence; | |
@class RACTokenizedStringSequence; | |
@interface NSString (RACSequenceAdditions) | |
/// Creates and returns a sequence containing strings corresponding to each | |
/// composed character sequence in the receiver. | |
/// | |
/// Mutating the receiver will not affect the sequence after it's been created. | |
@property (nonatomic, copy, readonly) RACSequence *rac_sequence; | |
@property (nonatomic, copy, readonly) RACSequence *rac_lineSequence; | |
- (RACSequence *)rac_sequenceWithTokenization:(NSStringEnumerationOptions)tokenization; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RACTokenizedStringSequence.m | |
// ReactiveCocoa | |
// | |
// Created by Petro Korienev on 6/8/16. | |
// Copyright © 2016 GitHub. All rights reserved. | |
// | |
#import "RACTokenizedStringSequence.h" | |
@interface RACTokenizedStringSequence () | |
@property (nonatomic, assign) NSStringEnumerationOptions tokenization; | |
@property (nonatomic, strong) NSString *headString; | |
@property (nonatomic, strong) NSString *tailString; | |
@end | |
@implementation RACTokenizedStringSequence | |
#pragma mark Lifecycle | |
+ (instancetype)sequenceWithString:(NSString *)string tokenization:(NSStringEnumerationOptions)tokenization { | |
if (!string.length) return self.empty; | |
__block NSString *head = nil; | |
__block NSString *tail = nil; | |
[string enumerateSubstringsInRange:NSMakeRange(0, string.length) | |
options:tokenization | |
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { | |
if (!head) { // first iteration | |
head = substring; | |
} | |
else { //second iteration | |
tail = [string substringWithRange:NSMakeRange(substringRange.location, string.length - substringRange.location)]; | |
*stop = YES; | |
} | |
}]; | |
RACTokenizedStringSequence *seq = [self new]; | |
seq.headString = head; | |
seq.tailString = tail; | |
seq.tokenization = tokenization; | |
return seq; | |
} | |
#pragma mark RACSequence | |
- (id)head { | |
return self.headString; | |
} | |
- (RACSequence *)tail { | |
RACSequence *sequence = [self.class sequenceWithString:self.tailString tokenization:self.tokenization]; | |
sequence.name = self.name; | |
return sequence; | |
} | |
- (NSArray *)array { | |
NSMutableArray *array = [NSMutableArray arrayWithObject:self.headString]; | |
[self.tailString enumerateSubstringsInRange:NSMakeRange(0, self.tailString.length) | |
options:self.tokenization | |
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) { | |
[array addObject:substring]; | |
}]; | |
return [array copy]; | |
} | |
#pragma mark NSObject | |
- (NSString *)description { | |
return [NSString stringWithFormat:@"<%@: %p>{ name = %@, string = %@ }", self.class, self, self.name, [self.headString stringByAppendingString:self.tailString]]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment