utatane.tea@gmail.com | bc0f630 | 2015-10-17 04:37:16 +0000 | [diff] [blame] | 1 | function unicode(value) { |
| 2 | return value.split('').map((val) => "\\u" + ("0000" + val.charCodeAt(0).toString(16)).slice(-4)).join(''); |
| 3 | } |
| 4 | |
| 5 | function shouldBe(actual, expected) { |
| 6 | if (actual !== expected) |
| 7 | throw new Error(`bad value: ${unicode(String(actual))}`); |
| 8 | } |
| 9 | |
| 10 | function shouldThrow(func, errorMessage) { |
| 11 | var errorThrown = false; |
| 12 | var error = null; |
| 13 | try { |
| 14 | func(); |
| 15 | } catch (e) { |
| 16 | errorThrown = true; |
| 17 | error = e; |
| 18 | } |
| 19 | if (!errorThrown) |
| 20 | throw new Error('not thrown'); |
| 21 | if (String(error) !== errorMessage) |
| 22 | throw new Error(`bad error: ${String(error)}`); |
| 23 | } |
| 24 | |
| 25 | shouldBe(String.prototype.hasOwnProperty('normalize'), true); |
| 26 | shouldBe(String.prototype.hasOwnProperty.length, 1); |
| 27 | shouldBe(Object.getOwnPropertyDescriptor(String.prototype, 'normalize').writable, true); |
| 28 | shouldBe(Object.getOwnPropertyDescriptor(String.prototype, 'normalize').enumerable, false); |
| 29 | shouldBe(Object.getOwnPropertyDescriptor(String.prototype, 'normalize').configurable, true); |
| 30 | |
| 31 | shouldThrow(() => { |
| 32 | "Test".normalize("Invalid"); |
| 33 | }, `RangeError: argument does not match any normalization form`); |
| 34 | |
| 35 | function normalizeTest(original, defaultValue, nfc, nfd, nfkc, nfkd) { |
| 36 | shouldBe(original.normalize(), defaultValue); |
| 37 | shouldBe(original.normalize("NFC"), nfc); |
| 38 | shouldBe(original.normalize("NFD"), nfd); |
| 39 | shouldBe(original.normalize("NFKC"), nfkc); |
| 40 | shouldBe(original.normalize("NFKD"), nfkd); |
| 41 | } |
| 42 | |
| 43 | { |
| 44 | let text = "Cocoa"; |
| 45 | normalizeTest(text, text, text, text, text, text); |
| 46 | } |
| 47 | |
| 48 | { |
| 49 | // うさぎ |
| 50 | // \u3046\u3055\u304e |
| 51 | let text = "\u3046\u3055\u304e"; |
| 52 | normalizeTest(text, text, text, "\u3046\u3055\u304d\u3099", text, "\u3046\u3055\u304d\u3099"); |
| 53 | } |
| 54 | |
| 55 | { |
| 56 | // é |
| 57 | let text = "\u00e9"; |
| 58 | normalizeTest(text, text, text, "\u0065\u0301", text, "\u0065\u0301"); |
| 59 | } |
| 60 | |
| 61 | { |
| 62 | // http://unicode.org/faq/normalization.html#6 |
| 63 | let text = "\u03d3"; |
| 64 | normalizeTest(text, text, text, "\u03d2\u0301", "\u038e", "\u03a5\u0301"); |
| 65 | } |
| 66 | { |
| 67 | // http://unicode.org/faq/normalization.html#6 |
| 68 | let text = "\u03d4"; |
| 69 | normalizeTest(text, text, text, "\u03d2\u0308", "\u03ab", "\u03a5\u0308"); |
| 70 | } |
| 71 | { |
| 72 | // http://unicode.org/faq/normalization.html#6 |
| 73 | let text = "\u1e9b"; |
| 74 | normalizeTest(text, text, text, "\u017f\u0307", "\u1e61", "\u0073\u0307"); |
| 75 | } |
| 76 | |
| 77 | { |
| 78 | // http://unicode.org/faq/normalization.html#6 |
| 79 | let text = "\u1e9b"; |
| 80 | normalizeTest(text, text, text, "\u017f\u0307", "\u1e61", "\u0073\u0307"); |
| 81 | } |
| 82 | |
| 83 | { |
| 84 | // http://unicode.org/faq/normalization.html#12 |
| 85 | normalizeTest("\ud834\udd60", |
| 86 | "\ud834\udd58\ud834\udd65\ud834\udd6e", |
| 87 | "\ud834\udd58\ud834\udd65\ud834\udd6e", |
| 88 | "\ud834\udd58\ud834\udd65\ud834\udd6e", |
| 89 | "\ud834\udd58\ud834\udd65\ud834\udd6e", |
| 90 | "\ud834\udd58\ud834\udd65\ud834\udd6e"); |
| 91 | normalizeTest("\uFB2C", |
| 92 | "\u05e9\u05bc\u05c1", |
| 93 | "\u05e9\u05bc\u05c1", |
| 94 | "\u05e9\u05bc\u05c1", |
| 95 | "\u05e9\u05bc\u05c1", |
| 96 | "\u05e9\u05bc\u05c1", |
| 97 | "\u05e9\u05bc\u05c1" |
| 98 | ); |
| 99 | normalizeTest("\u0390", |
| 100 | "\u0390", |
| 101 | "\u0390", |
| 102 | "\u03b9\u0308\u0301", |
| 103 | "\u0390", |
| 104 | "\u03b9\u0308\u0301" |
| 105 | ); |
| 106 | normalizeTest("\u1F82", |
| 107 | "\u1f82", |
| 108 | "\u1f82", |
| 109 | "\u03b1\u0313\u0300\u0345", |
| 110 | "\u1f82", |
| 111 | "\u03b1\u0313\u0300\u0345" |
| 112 | ); |
| 113 | normalizeTest("\uFDFA", |
| 114 | "\ufdfa", |
| 115 | "\ufdfa", |
| 116 | "\ufdfa", |
| 117 | "\u0635\u0644\u0649\u0020\u0627\u0644\u0644\u0647\u0020\u0639\u0644\u064a\u0647\u0020\u0648\u0633\u0644\u0645", |
| 118 | "\u0635\u0644\u0649\u0020\u0627\u0644\u0644\u0647\u0020\u0639\u0644\u064a\u0647\u0020\u0648\u0633\u0644\u0645" |
| 119 | ); |
| 120 | } |