NativeStringTools.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package js.lib;
  23. import haxe.extern.EitherType;
  24. import js.Lib.undefined;
  25. import js.Syntax;
  26. import js.lib.intl.Collator.CollatorOptions;
  27. /**
  28. Documentation [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) by [Mozilla Contributors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String$history), licensed under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/).
  29. **/
  30. class NativeStringTools {
  31. /**
  32. Returns the Unicode Normalization Form of the calling string value.
  33. */
  34. public static inline function normalize(string:String, form:NormalizationForm):String {
  35. return Syntax.code("{0}.normalize({1})", string, form);
  36. }
  37. /**
  38. Returns a number indicating whether a reference string comes before or after or is
  39. the same as the given string in sort order.
  40. */
  41. public static inline function localeCompare(string:String, compareString:String, ?locales:EitherType<String, Array<String>>, ?options:CollatorOptions):Bool {
  42. return Syntax.code(
  43. "{0}.localeCompare({1}, {2}, {3})",
  44. string,
  45. (compareString != null) ? compareString : undefined,
  46. (locales != null) ? locales : undefined,
  47. (options != null) ? options : undefined
  48. );
  49. }
  50. /**
  51. The characters within a string are converted to lower case while respecting the current locale.
  52. For most languages, this will return the same as toLowerCase().
  53. */
  54. public static inline function toLocaleLowerCase(string:String, ?locales:EitherType<String, Array<String>>):String {
  55. return Syntax.code(
  56. "{0}.toLocaleLowerCase({1})",
  57. string,
  58. (locales != null) ? locales : undefined
  59. );
  60. }
  61. /**
  62. The characters within a string are converted to upper case while respecting the current locale.
  63. For most languages, this will return the same as toUpperCase().
  64. */
  65. public static inline function toLocaleUpperCase(string:String, ?locales:EitherType<String, Array<String>>):String {
  66. return Syntax.code(
  67. "{0}.toLocaleUpperCase({1})",
  68. string,
  69. (locales != null) ? locales : undefined
  70. );
  71. }
  72. /**
  73. The `charCodeAt()` method of String values returns an integer between 0
  74. and 65535 representing the UTF-16 code unit at the given index.
  75. If `index` is out of range of `0` – `str.length - 1`, returns `NaN`.
  76. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
  77. */
  78. public static inline function charCodeAt(string:String, index:Int):Int {
  79. return Syntax.code("{0}.charCodeAt({1})", string, index);
  80. }
  81. /**
  82. The `replace()` method of String values returns a new string with one,
  83. some, or all matches of a pattern replaced by a replacement.
  84. The pattern can be a `String` or a `js.lib.RegExp`, and the replacement
  85. can be a string or a function called for each match.
  86. If pattern is a string, only the first occurrence will be replaced. The
  87. original string is left unchanged.
  88. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
  89. */
  90. @:overload(function(str:String, pattern:RegExp, replacement:String):String {})
  91. @:overload(function(str:String, pattern:RegExp, replacement:String->String):String {})
  92. @:overload(function(str:String, pattern:String, replacement:String->String):String {})
  93. public static function replace(string:String, pattern:String, replacement:String):String {
  94. return Syntax.code("{0}.replace({1}, {2})", string, pattern, replacement);
  95. }
  96. }
  97. enum abstract NormalizationForm(String) {
  98. /**
  99. Normalization Form Canonical Composition.
  100. */
  101. var NFC;
  102. /**
  103. Normalization Form Canonical Decomposition.
  104. */
  105. var NFD;
  106. /**
  107. Normalization Form Compatibility Composition.
  108. */
  109. var NFKC;
  110. /**
  111. Normalization Form Compatibility Decomposition.
  112. */
  113. var NFKD;
  114. }