Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pablohirafuji/elm-char-codepoint
Code point operations in Elm
https://github.com/pablohirafuji/elm-char-codepoint
char codepoints elm
Last synced: 9 days ago
JSON representation
Code point operations in Elm
- Host: GitHub
- URL: https://github.com/pablohirafuji/elm-char-codepoint
- Owner: pablohirafuji
- License: mit
- Created: 2017-03-10T19:43:00.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-03-17T19:28:22.000Z (almost 8 years ago)
- Last Synced: 2024-11-08T06:19:46.132Z (2 months ago)
- Topics: char, codepoints, elm
- Language: Elm
- Homepage: http://package.elm-lang.org/packages/pablohirafuji/elm-char-codepoint/latest
- Size: 116 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Code Point
Convert char to code point and code point to string, similar to javascript's `String.codePointAt` and `String.fromCodePoint`. [Demo](https://pablohirafuji.github.io/elm-char-codepoint/).
## What's the difference between `Char.toCode` and `Char.CodePoint.fromChar`?
From [MDN](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt):
> The `charCodeAt()` method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index (the UTF-16 code unit matches the Unicode code point for code points representable in a single UTF-16 code unit, but might also be the first code unit of a surrogate pair for code points not representable in a single UTF-16 code unit, e.g. Unicode code points > 0x10000). If you want the entire code point value, use `codePointAt()`.
This mean that not all chars exists in a single 16 bits representation, and `Char.toCode` only returns the first 16 bits code of any given char.
Here is an example to demonstrate how this can affect the output of `Char.toCode` and how `Char.CodePoint.fromChar` fix that:
```elm
Char.toCode '𝔸' == 55349
Char.fromCode 55349 == '�'Char.CodePoint.fromChar '𝔸' == 120120
Char.CodePoint.toString 120120 == "𝔸"
```You can try different chars in this [demo](https://pablohirafuji.github.io/elm-char-codepoint/).