Skip to content

Instantly share code, notes, and snippets.

@dmsnell
Last active July 5, 2020 01:01
Show Gist options
  • Save dmsnell/e0c95477bc698ad281710746e77a475f to your computer and use it in GitHub Desktop.
Save dmsnell/e0c95477bc698ad281710746e77a475f to your computer and use it in GitHub Desktop.
Simplenote Tag Hashing Functions - generate tag entity id from tag name
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.Normalizer;
import java.util.Locale;
class TagHasher {
public static String tagHash(String tagName) {
try {
String normalized = Normalizer.normalize(tagName, Normalizer.Form.NFC);
String lowercased = normalized.toLowerCase(Locale.ENGLISH);
String encoded = URLEncoder.encode(lowercased, "UTF-8");
return encoded.replace("*", "%2A").replace("+", "%20");
} catch (UnsupportedEncodingException e) {
// @TODO handle this with a custom UTF-8 encoder
return tagName;
}
}
}
#import <Foundation/Foundation.h>
NSString *tagHash(NSString *tagName) {
NSString *normalized = [tagName precomposedStringWithCanonicalMapping];
NSString *lowercased = [normalized lowercaseStringWithLocale:[NSLocale localeWithLocaleIdentifier:@"en-US"]];
NSString *encoded = [lowercased stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
return encoded;
}
import Foundation
func tagHash(tagName: String) -> String {
let normalized = (tagName as NSString).precomposedStringWithCanonicalMapping
let lowercased = normalized.lowercased(with: Locale(identifier: "en-US"))
if let encoded = lowercased.addingPercentEncoding(withAllowedCharacters:CharacterSet.alphanumerics) {
return encoded
}
// @TODO: handle failure with custom encoder
return tagName
}
export const tagHash = (tagName: string): string => {
const normalized = tagName.normalize('NFC');
const lowercased = normalized.toLocaleLowerCase('en-US');
const encoded = encodeURIComponent(lowercased);
return encoded.replace(/[!'()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment