ListFormat.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2021-2024, 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/Iterator.h>
  11. #include <LibUnicode/ListFormat.h>
  12. namespace JS::Intl {
  13. JS_DEFINE_ALLOCATOR(ListFormat);
  14. // 13 ListFormat Objects, https://tc39.es/ecma402/#listformat-objects
  15. ListFormat::ListFormat(Object& prototype)
  16. : Object(ConstructWithPrototypeTag::Tag, prototype)
  17. {
  18. }
  19. // 13.5.2 CreatePartsFromList ( listFormat, list ), https://tc39.es/ecma402/#sec-createpartsfromlist
  20. Vector<Unicode::ListFormat::Partition> create_parts_from_list(ListFormat const& list_format, ReadonlySpan<String> list)
  21. {
  22. return list_format.formatter().format_to_parts(list);
  23. }
  24. // 13.5.3 FormatList ( listFormat, list ), https://tc39.es/ecma402/#sec-formatlist
  25. String format_list(ListFormat const& list_format, ReadonlySpan<String> list)
  26. {
  27. // 1. Let parts be ! CreatePartsFromList(listFormat, list).
  28. // 2. Let result be the empty String.
  29. // 3. For each Record { [[Type]], [[Value]] } part in parts, do
  30. // a. Set result to the string-concatenation of result and part.[[Value]].
  31. // 4. Return result.
  32. return list_format.formatter().format(list);
  33. }
  34. // 13.5.4 FormatListToParts ( listFormat, list ), https://tc39.es/ecma402/#sec-formatlisttoparts
  35. NonnullGCPtr<Array> format_list_to_parts(VM& vm, ListFormat const& list_format, ReadonlySpan<String> list)
  36. {
  37. auto& realm = *vm.current_realm();
  38. // 1. Let parts be ! CreatePartsFromList(listFormat, list).
  39. auto parts = create_parts_from_list(list_format, list);
  40. // 2. Let result be ! ArrayCreate(0).
  41. auto result = MUST(Array::create(realm, 0));
  42. // 3. Let n be 0.
  43. size_t n = 0;
  44. // 4. For each Record { [[Type]], [[Value]] } part in parts, do
  45. for (auto& part : parts) {
  46. // a. Let O be OrdinaryObjectCreate(%Object.prototype%).
  47. auto object = Object::create(realm, realm.intrinsics().object_prototype());
  48. // b. Perform ! CreateDataPropertyOrThrow(O, "type", part.[[Type]]).
  49. MUST(object->create_data_property_or_throw(vm.names.type, PrimitiveString::create(vm, part.type)));
  50. // c. Perform ! CreateDataPropertyOrThrow(O, "value", part.[[Value]]).
  51. MUST(object->create_data_property_or_throw(vm.names.value, PrimitiveString::create(vm, move(part.value))));
  52. // d. Perform ! CreateDataPropertyOrThrow(result, ! ToString(n), O).
  53. MUST(result->create_data_property_or_throw(n, object));
  54. // e. Increment n by 1.
  55. ++n;
  56. }
  57. // 5. Return result.
  58. return result;
  59. }
  60. // 13.5.5 StringListFromIterable ( iterable ), https://tc39.es/ecma402/#sec-createstringlistfromiterable
  61. ThrowCompletionOr<Vector<String>> string_list_from_iterable(VM& vm, Value iterable)
  62. {
  63. // 1. If iterable is undefined, then
  64. if (iterable.is_undefined()) {
  65. // a. Return a new empty List.
  66. return Vector<String> {};
  67. }
  68. // 2. Let iteratorRecord be ? GetIterator(iterable, sync).
  69. auto iterator_record = TRY(get_iterator(vm, iterable, IteratorHint::Sync));
  70. // 3. Let list be a new empty List.
  71. Vector<String> list;
  72. // 4. Repeat,
  73. while (true) {
  74. // a. Let next be ? IteratorStepValue(iteratorRecord).
  75. auto next = TRY(iterator_step_value(vm, iterator_record));
  76. // b. If next is DONE, then
  77. if (!next.has_value()) {
  78. // a. Return list.
  79. return list;
  80. }
  81. // c. If Type(next) is not String, then
  82. if (!next->is_string()) {
  83. // 1. Let error be ThrowCompletion(a newly created TypeError object).
  84. auto error = vm.throw_completion<TypeError>(ErrorType::NotAString, *next);
  85. // 2. Return ? IteratorClose(iteratorRecord, error).
  86. return iterator_close(vm, iterator_record, move(error));
  87. }
  88. // iii. Append next to list.
  89. list.append(next->as_string().utf8_string());
  90. }
  91. }
  92. }