ListFormat.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <LibJS/Runtime/Array.h>
  8. #include <LibJS/Runtime/GlobalObject.h>
  9. #include <LibJS/Runtime/Intl/ListFormat.h>
  10. #include <LibJS/Runtime/IteratorOperations.h>
  11. namespace JS::Intl {
  12. // 13 ListFormat Objects, https://tc39.es/ecma402/#listformat-objects
  13. ListFormat::ListFormat(Object& prototype)
  14. : Object(prototype)
  15. {
  16. }
  17. void ListFormat::set_type(StringView type)
  18. {
  19. if (type == "conjunction"sv) {
  20. m_type = Type::Conjunction;
  21. } else if (type == "disjunction"sv) {
  22. m_type = Type::Disjunction;
  23. } else if (type == "unit"sv) {
  24. m_type = Type::Unit;
  25. } else {
  26. VERIFY_NOT_REACHED();
  27. }
  28. }
  29. StringView ListFormat::type_string() const
  30. {
  31. switch (m_type) {
  32. case Type::Conjunction:
  33. return "conjunction"sv;
  34. case Type::Disjunction:
  35. return "disjunction"sv;
  36. case Type::Unit:
  37. return "unit"sv;
  38. default:
  39. VERIFY_NOT_REACHED();
  40. }
  41. }
  42. // 13.5.1 DeconstructPattern ( pattern, placeables ), https://tc39.es/ecma402/#sec-deconstructpattern
  43. Vector<PatternPartition> deconstruct_pattern(StringView pattern, Placeables placeables)
  44. {
  45. // 1. Let patternParts be ! PartitionPattern(pattern).
  46. auto pattern_parts = partition_pattern(pattern);
  47. // 2. Let result be a new empty List.
  48. Vector<PatternPartition> result {};
  49. // 3. For each Record { [[Type]], [[Value]] } patternPart of patternParts, do
  50. for (auto& pattern_part : pattern_parts) {
  51. // a. Let part be patternPart.[[Type]].
  52. auto part = pattern_part.type;
  53. // b. If part is "literal", then
  54. if (part == "literal"sv) {
  55. // i. Append Record { [[Type]]: "literal", [[Value]]: patternPart.[[Value]] } to result.
  56. result.append({ part, move(pattern_part.value) });
  57. }
  58. // c. Else,
  59. else {
  60. // i. Assert: placeables has a field [[<part>]].
  61. // ii. Let subst be placeables.[[<part>]].
  62. auto subst = placeables.get(part);
  63. VERIFY(subst.has_value());
  64. subst.release_value().visit(
  65. // iii. If Type(subst) is List, then
  66. [&](Vector<PatternPartition>& partition) {
  67. // 1. For each element s of subst, do
  68. for (auto& element : partition) {
  69. // a. Append s to result.
  70. result.append(move(element));
  71. }
  72. },
  73. // iv. Else,
  74. [&](PatternPartition& partition) {
  75. // 1. Append subst to result.
  76. result.append(move(partition));
  77. });
  78. }
  79. }
  80. // 4. Return result.
  81. return result;
  82. }
  83. // 13.5.2 CreatePartsFromList ( listFormat, list ), https://tc39.es/ecma402/#sec-createpartsfromlist
  84. Vector<PatternPartition> create_parts_from_list(ListFormat const& list_format, Vector<String> const& list)
  85. {
  86. auto list_patterns = Unicode::get_locale_list_patterns(list_format.locale(), list_format.type_string(), list_format.style());
  87. if (!list_patterns.has_value())
  88. return {};
  89. // 1. Let size be the number of elements of list.
  90. auto size = list.size();
  91. // 2. If size is 0, then
  92. if (size == 0) {
  93. // a. Return a new empty List.
  94. return {};
  95. }
  96. // 3. If size is 2, then
  97. if (size == 2) {
  98. // a. Let n be an index into listFormat.[[Templates]] based on listFormat.[[Locale]], list[0], and list[1].
  99. // b. Let pattern be listFormat.[[Templates]][n].[[Pair]].
  100. auto pattern = list_patterns->pair;
  101. // c. Let first be a new Record { [[Type]]: "element", [[Value]]: list[0] }.
  102. PatternPartition first { "element"sv, list[0] };
  103. // d. Let second be a new Record { [[Type]]: "element", [[Value]]: list[1] }.
  104. PatternPartition second { "element"sv, list[1] };
  105. // e. Let placeables be a new Record { [[0]]: first, [[1]]: second }.
  106. Placeables placeables;
  107. placeables.set("0"sv, move(first));
  108. placeables.set("1"sv, move(second));
  109. // f. Return ! DeconstructPattern(pattern, placeables).
  110. return deconstruct_pattern(pattern, move(placeables));
  111. }
  112. // 4. Let last be a new Record { [[Type]]: "element", [[Value]]: list[size - 1] }.
  113. PatternPartition last { "element"sv, list[size - 1] };
  114. // 5. Let parts be « last ».
  115. Vector<PatternPartition> parts { move(last) };
  116. // The spec does not say to do this, but because size_t is unsigned, we need to take care not to wrap around 0.
  117. if (size == 1)
  118. return parts;
  119. // 6. Let i be size - 2.
  120. size_t i = size - 2;
  121. // 7. Repeat, while i ≥ 0,
  122. do {
  123. // a. Let head be a new Record { [[Type]]: "element", [[Value]]: list[i] }.
  124. PatternPartition head { "element"sv, list[i] };
  125. // b. Let n be an implementation-defined index into listFormat.[[Templates]] based on listFormat.[[Locale]], head, and parts.
  126. StringView pattern;
  127. // c. If i is 0, then
  128. if (i == 0) {
  129. // i. Let pattern be listFormat.[[Templates]][n].[[Start]].
  130. pattern = list_patterns->start;
  131. }
  132. // d. Else if i is less than size - 2, then
  133. else if (i < (size - 2)) {
  134. // i. Let pattern be listFormat.[[Templates]][n].[[Middle]].
  135. pattern = list_patterns->middle;
  136. }
  137. // e. Else,
  138. else {
  139. // i. Let pattern be listFormat.[[Templates]][n].[[End]].
  140. pattern = list_patterns->end;
  141. }
  142. // f. Let placeables be a new Record { [[0]]: head, [[1]]: parts }.
  143. Placeables placeables;
  144. placeables.set("0"sv, move(head));
  145. placeables.set("1"sv, move(parts));
  146. // g. Set parts to ! DeconstructPattern(pattern, placeables).
  147. parts = deconstruct_pattern(pattern, move(placeables));
  148. // h. Decrement i by 1.
  149. } while (i-- != 0);
  150. // 8. Return parts.
  151. return parts;
  152. }
  153. // 13.5.3 FormatList ( listFormat, list ), https://tc39.es/ecma402/#sec-formatlist
  154. String format_list(ListFormat const& list_format, Vector<String> const& list)
  155. {
  156. // 1. Let parts be ! CreatePartsFromList(listFormat, list).
  157. auto parts = create_parts_from_list(list_format, list);
  158. // 2. Let result be an empty String.
  159. StringBuilder result;
  160. // 3. For each Record { [[Type]], [[Value]] } part in parts, do
  161. for (auto& part : parts) {
  162. // a. Set result to the string-concatenation of result and part.[[Value]].
  163. result.append(move(part.value));
  164. }
  165. // 4. Return result.
  166. return result.build();
  167. }
  168. // 13.5.4 FormatListToParts ( listFormat, list ), https://tc39.es/ecma402/#sec-formatlisttoparts
  169. Array* format_list_to_parts(VM& vm, ListFormat const& list_format, Vector<String> const& list)
  170. {
  171. auto& realm = *vm.current_realm();
  172. auto& global_object = realm.global_object();
  173. // 1. Let parts be ! CreatePartsFromList(listFormat, list).
  174. auto parts = create_parts_from_list(list_format, list);
  175. // 2. Let result be ! ArrayCreate(0).
  176. auto* result = MUST(Array::create(realm, 0));
  177. // 3. Let n be 0.
  178. size_t n = 0;
  179. // 4. For each Record { [[Type]], [[Value]] } part in parts, do
  180. for (auto& part : parts) {
  181. // a. Let O be OrdinaryObjectCreate(%Object.prototype%).
  182. auto* object = Object::create(realm, global_object.object_prototype());
  183. // b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
  184. MUST(object->create_data_property_or_throw(vm.names.type, js_string(vm, part.type)));
  185. // c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
  186. MUST(object->create_data_property_or_throw(vm.names.value, js_string(vm, move(part.value))));
  187. // d. Perform ! CreateDataPropertyOrThrow(result, ! ToString(n), O).
  188. MUST(result->create_data_property_or_throw(n, object));
  189. // e. Increment n by 1.
  190. ++n;
  191. }
  192. // 5. Return result.
  193. return result;
  194. }
  195. // 13.5.5 StringListFromIterable ( iterable ), https://tc39.es/ecma402/#sec-createstringlistfromiterable
  196. ThrowCompletionOr<Vector<String>> string_list_from_iterable(VM& vm, Value iterable)
  197. {
  198. // 1. If iterable is undefined, then
  199. if (iterable.is_undefined()) {
  200. // a. Return a new empty List.
  201. return Vector<String> {};
  202. }
  203. // 2. Let iteratorRecord be ? GetIterator(iterable).
  204. auto iterator_record = TRY(get_iterator(vm, iterable));
  205. // 3. Let list be a new empty List.
  206. Vector<String> list;
  207. // 4. Let next be true.
  208. Object* next = nullptr;
  209. // 5. Repeat, while next is not false,
  210. do {
  211. // a. Set next to ? IteratorStep(iteratorRecord).
  212. next = TRY(iterator_step(vm, iterator_record));
  213. // b. If next is not false, then
  214. if (next != nullptr) {
  215. // i. Let nextValue be ? IteratorValue(next).
  216. auto next_value = TRY(iterator_value(vm, *next));
  217. // ii. If Type(nextValue) is not String, then
  218. if (!next_value.is_string()) {
  219. // 1. Let error be ThrowCompletion(a newly created TypeError object).
  220. auto error = vm.throw_completion<TypeError>(ErrorType::NotAString, next_value);
  221. // 2. Return ? IteratorClose(iteratorRecord, error).
  222. return iterator_close(vm, iterator_record, move(error));
  223. }
  224. // iii. Append nextValue to the end of the List list.
  225. list.append(next_value.as_string().string());
  226. }
  227. } while (next != nullptr);
  228. // 6. Return list.
  229. return list;
  230. }
  231. }