Last active
July 5, 2020 01:01
-
-
Save dmsnell/e0c95477bc698ad281710746e77a475f to your computer and use it in GitHub Desktop.
Simplenote Tag Hashing Functions - generate tag entity id from tag name
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
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; | |
} | |
} | |
} |
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
#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; | |
} |
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
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 | |
} |
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
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