Skip to content

Instantly share code, notes, and snippets.

extension String {
var isHexColorString: Bool {
do {
let regex = try NSRegularExpression(pattern: "^#?[0-9A-Fa-f]{6}$")
return regex.matches(in: self, options: [], range: wholeRange).isEmpty == false
} catch {
return false
}
}
}
@commanda
commanda / UIView+AllSubviews.swift
Created December 20, 2018 20:55
get all subviews of a UIView, recursively
extension UIView {
var allSubviews: [UIView] {
return self.subviews.reduce(into: [self]) { array, subview in
array += subview.allSubviews
}
}
}

Keybase proof

I hereby claim:

  • I am commanda on github.
  • I am commanda (https://keybase.io/commanda) on keybase.
  • I have a public key whose fingerprint is 74DB ACD4 CD18 915F E6E2 7C50 4E57 2F26 885B CA13

To claim this, I am signing this object:

@commanda
commanda / promises_reduce.js
Last active September 6, 2016 21:50 — forked from anvk/promises_reduce.js
Sequential execution of Promises using reduce()
function createPromiseForFunction(e, functionThatTakesOneItem)
{
return new Promise((resolve, reject) => {
resolve(functionThatTakesOneItem(e));
});
}
@commanda
commanda / Traverse.m
Created July 27, 2016 15:51
traverse a json-ish structure in objective-c (and delete certain elements from it)
+ (BOOL)traverse:(NSObject *)obj comparisonBlock:(BOOL (^)(NSString *, NSObject *))comparisonBlock;
{
if([obj isKindOfClass:[NSMutableArray class]])
{
NSMutableArray *array = (NSMutableArray *)obj;
NSMutableArray *toDelete = [@[] mutableCopy];
for(NSObject *element in array)
{
@commanda
commanda / traverse.js
Created July 27, 2016 15:49
traverse a json-ish structure in javascript
function traverse(object, block)
{
for(var i in object)
{
block(typeof(object[i]) !== "object", i, object[i]);
if(object[i] !== null && typeof(object[i]) !== "string")
{
traverse(object[i], block);
}
}
@commanda
commanda / traverse.py
Created July 27, 2016 15:48
traverse a json-esque structure in python
def value_generator(data):
if isinstance(data, dict):
for key, value in data.iteritems():
for element in value_generator(value):
yield element
elif isinstance(data, list):
for sublist in data:
for element in value_generator(sublist):
yield element
else:
@commanda
commanda / _clang_format
Last active June 26, 2016 07:08
My _clang_format file for formatting Objective-C code
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
@commanda
commanda / gist:62cd8bd1928306329b5e3fe02edd51ef
Created June 2, 2016 16:18 — forked from quietcricket/gist:1593632
Fuzzy string match objective-c (Levenshtein Distance Algorithm)
-(float)compareString:(NSString *)originalString withString:(NSString *)comparisonString
{
// Normalize strings
[originalString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[comparisonString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
originalString = [originalString lowercaseString];
comparisonString = [comparisonString lowercaseString];
// Step 1 (Steps follow description at http://www.merriampark.com/ld.htm)
@commanda
commanda / gist:250419ae9b386fa05e47bc38d3931507
Created May 30, 2016 20:55
A recursive block in Objective-C
- (void)runRecursiveBlock
{
void(^ completionBlock) ();
void(^ __block __weak weakCompletionBlock) ();
weakCompletionBlock = completionBlock = ^{
NSLog(@"%@", NSStringFromSelector(_cmd));
void(^ strongCompletionBlock)() = weakCompletionBlock;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
strongCompletionBlock();
});