Last active
March 28, 2018 21:28
-
-
Save cmkilger/a0d097fc04b2d4ceef17 to your computer and use it in GitHub Desktop.
Detects if a string is made up of just emoji (and space).
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 | |
extension String { | |
private static let regex = try! NSRegularExpression(pattern: "^[\\s\n\r]*(?:(?:[\u{a9}\u{ae}\u{203c}\u{2049}\u{2122}\u{2139}\u{2194}-\u{2199}\u{21a9}-\u{21aa}\u{231a}-\u{231b}\u{2328}\u{23cf}\u{23e9}-\u{23f3}\u{23f8}-\u{23fa}\u{24c2}\u{25aa}-\u{25ab}\u{25b6}\u{25c0}\u{25fb}-\u{25fe}\u{2600}-\u{2604}\u{260e}\u{2611}\u{2614}-\u{2615}\u{2618}\u{261d}\u{2620}\u{2622}-\u{2623}\u{2626}\u{262a}\u{262e}-\u{262f}\u{2638}-\u{263a}\u{2648}-\u{2653}\u{2660}\u{2663}\u{2665}-\u{2666}\u{2668}\u{267b}\u{267f}\u{2692}-\u{2694}\u{2696}-\u{2697}\u{2699}\u{269b}-\u{269c}\u{26a0}-\u{26a1}\u{26aa}-\u{26ab}\u{26b0}-\u{26b1}\u{26bd}-\u{26be}\u{26c4}-\u{26c5}\u{26c8}\u{26ce}-\u{26cf}\u{26d1}\u{26d3}-\u{26d4}\u{26e9}-\u{26ea}\u{26f0}-\u{26f5}\u{26f7}-\u{26fa}\u{26fd}\u{2702}\u{2705}\u{2708}-\u{270d}\u{270f}\u{2712}\u{2714}\u{2716}\u{271d}\u{2721}\u{2728}\u{2733}-\u{2734}\u{2744}\u{2747}\u{274c}\u{274e}\u{2753}-\u{2755}\u{2757}\u{2763}-\u{2764}\u{2795}-\u{2797}\u{27a1}\u{27b0}\u{27bf}\u{2934}-\u{2935}\u{2b05}-\u{2b07}\u{2b1b}-\u{2b1c}\u{2b50}\u{2b55}\u{3030}\u{303d}\u{3297}\u{3299}\u{1f004}\u{1f0cf}\u{1f170}-\u{1f171}\u{1f17e}-\u{1f17f}\u{1f18e}\u{1f191}-\u{1f19a}\u{1f201}-\u{1f202}\u{1f21a}\u{1f22f}\u{1f232}-\u{1f23a}\u{1f250}-\u{1f251}\u{200d}\u{1F300}-\u{1F5FF}\u{1F600}-\u{1F64F}\u{1F680}-\u{1F6FF}\u{1F900}-\u{1F9FF}\u{e0020}-\u{e007f}]|\u{200d}[\u{2640}\u{2642}]|[\u{1f1e6}-\u{1f1ff}]{2}|.[\u{20e0}\u{20e3}\u{fe0f}]+)+[\\s\n\r]*)+$", options: []) | |
var isEmojiOnly: Bool { | |
return (String.regex.firstMatchInString(self, options: [], range: NSMakeRange(0, (self as NSString).length)) != nil) | |
} | |
} | |
func test(string: String, _ isEmoji: Bool) { | |
if string.isEmojiOnly != isEmoji { | |
print("\(string) should\((isEmoji ? "" : " not")) be emoji only") | |
} | |
} | |
test("Hello ๐ฒ", false) | |
test("-", false) | |
test("+", false) | |
test("$", false) | |
test("๐ด๐ป", true) // Requires Unicode 8.0 support for skin tones | |
test(" ๐จโ๐จโ๐งโ๐ง ", true) | |
test(" 0๏ธโฃ โค๏ธ", true) | |
test("๐จโ๐จโ๐งโ๐ง โค๏ธ", true) | |
test(" 0๏ธโฃ โค๏ธ๐จโ๐จโ๐งโ๐ง ", true) | |
test("0๏ธโฃ\nโค๏ธ", true) | |
test("ยฉยฎ๐๐๐๐ฑโโโยฎยฉโข๐๐ด๐ตโซ๏ธโช๏ธ๐น๐๐ธโช๏ธโซ๏ธโป๏ธโพ๏ธ๐๐โฆ๏ธโฅ๏ธโฃ๏ธโ ๏ธ๐๏ธ๐ด๐โ๐จ๐ญ๐ฏ๐๐๐๐๐๐ฅ๐ฌ๐โชโช๐โ๏ธ#๏ธโฃ#๏ธโฃโคต๏ธ*๏ธโฃโน๏ธโฉ๏ธโช๏ธโ๏ธโ๏ธโก๏ธ๐โ๏ธ๐ผโธโฏ4๏ธโฃ๐๐๐ถ๐๐๐ง๐๐ โ ใฝ๏ธ๐ฏโ๏ธโ๐ณ๐ฏ๐ท๐ซโโญ๏ธ๐ขโ๏ธ๐ ฑใ๏ธโด๏ธ๐๐๐๐๐๐๐๐ฉ๐๐โฑ๐๐ฟ๐๐ท๐ฌ๐บ๐กโ๐ซ๐ ๐ ๐๐ด๐ฏ๐กโฒ๐ป๐๐ธ๐พ๐ฑ๐ฒ๐๐ฏ๐๐๐๐๐ข๐ฅ๐ฆ๐บ๐ค๐๐ ๐๐๐๐๐๐ท๐บ๐ด๐พ๐ฑ๐๐๐ถ๐ต๐ฟ๐๐ก๐๐ญ๐ ๐๐โ๏ธโ๐จโ๏ธโโญ๏ธ๐๐๐๐๐๐๐๐บ๐๐ธ๐๐ด๐๐๐๐๐ฆ๐๐ฝ๐ญ๐๐ถ๐๐ ๐๐จโ๐ฉโ๐งโ๐ฆ๐ฉโโค๏ธโ๐ฉ๐๐ผ๐๐ฝ๐ฅ๐๐ป๐๐ป๐ฟ๐ค๐ฒ๐ช๐๐๐๐๐๐๐ด๐๐๐ป๐ฒ๐", true) | |
test("ใ", false) | |
test("ใท", false) | |
test("ไท", false) | |
test("ไทซ", false) | |
test("ไทฝ", false) | |
test("๊", false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added support for flag tags.