-
Notifications
You must be signed in to change notification settings - Fork 3
/
list.ex
310 lines (235 loc) · 8.81 KB
/
list.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
defmodule Cldr.List do
@moduledoc """
Cldr module to formats lists.
If we have a list of days like `["Monday", "Tuesday", "Wednesday"]`
then we can format that list for a given locale by:
iex> Cldr.List.to_string(["Monday", "Tuesday", "Wednesday"], MyApp.Cldr, locale: "en")
{:ok, "Monday, Tuesday, and Wednesday"}
"""
@type pattern_type :: :or | :or_narrow | :or_short | :standard | :standard_narrow |
:standard_short | :unit | :unit_narrow | :unit_short
@doc """
Formats a list into a string according to the list pattern rules for a locale.
## Arguments
* `list` is any list of of terms that can be passed through `Kernel.to_string/1`
* `options` is a keyword list
## Options
* `:locale` is any configured locale. See `Cldr.known_locales()`. The default
is `locale: Cldr.get_locale/1`
* `:format` is one of those returned by
`Cldr.List.known_list_formats/0`. The default is `format: :standard`
## Examples
iex> Cldr.List.to_string(["a", "b", "c"], MyApp.Cldr, locale: "en")
{:ok, "a, b, and c"}
iex> Cldr.List.to_string(["a", "b", "c"], MyApp.Cldr, locale: "en", format: :unit_narrow)
{:ok, "a b c"}
iex> Cldr.List.to_string(["a", "b", "c"], MyApp.Cldr, locale: "fr")
{:ok, "a, b et c"}
iex> Cldr.List.to_string([1,2,3,4,5,6], MyApp.Cldr)
{:ok, "1, 2, 3, 4, 5, and 6"}
iex> Cldr.List.to_string(["a"], MyApp.Cldr)
{:ok, "a"}
iex> Cldr.List.to_string([1,2], MyApp.Cldr)
{:ok, "1 and 2"}
"""
@spec to_string([term(), ...], Cldr.backend() | Keyword.t(), Keyword.t()) ::
{:ok, String.t()} | {:error, {atom, binary}}
def to_string(list, backend \\ default_backend(), options \\ [])
def to_string(list, options, []) when is_list(options) do
{backend, options} = Keyword.pop(options, :backend, default_backend())
to_string(list, backend, options)
end
def to_string(list, backend, options) do
module = Module.concat(backend, List)
module.to_string(list, options)
end
@doc """
Formats a list using `to_string/2` but raises if there is
an error.
## Examples
iex> Cldr.List.to_string!(["a", "b", "c"], MyApp.Cldr, locale: "en")
"a, b, and c"
iex> Cldr.List.to_string!(["a", "b", "c"], MyApp.Cldr, locale: "en", format: :unit_narrow)
"a b c"
"""
@spec to_string!([term(), ...], Cldr.backend() | Keyword.t(), Keyword.t()) :: String.t() | no_return()
def to_string!(list, backend \\ default_backend(), options \\ [])
def to_string!(list, options, []) when is_list(options) do
{_locale, backend} = Cldr.locale_and_backend_from(options)
to_string!(list, backend, options)
end
def to_string!(list, backend, options) do
{_locale, backend} = Cldr.locale_and_backend_from(options[:locale], backend)
module = Module.concat(backend, List)
module.to_string!(list, options)
end
@doc """
Intersperces a list elements into a list format according to the list
pattern rules for a locale.
This function can be helpful when creating a list from `Phoenix`
safe strings which are of the format `{:safe, "some string"}`
## Arguments
* `list` is any list of of terms
* `options` is a keyword list
## Options
* `:locale` is any configured locale. See `Cldr.known_locale_names/1`. The default
is `locale: Cldr.get_locale/1`
* `:format` is atom returned by
`Cldr.List.known_list_formats/0`. The default is `:standard`
## Examples
iex> Cldr.List.intersperse(["a", "b", "c"], MyApp.Cldr, locale: "en")
{:ok, ["a", ", ", "b", ", and ", "c"]}
iex> Cldr.List.intersperse(["a", "b", "c"], MyApp.Cldr, locale: "en", format: :unit_narrow)
{:ok, ["a", " ", "b", " ", "c"]}
iex> Cldr.List.intersperse(["a", "b", "c"], MyApp.Cldr, locale: "fr")
{:ok, ["a", ", ", "b", " et ", "c"]}
iex> Cldr.List.intersperse([1,2,3,4,5,6], MyApp.Cldr)
{:ok, [1, ", ", 2, ", ", 3, ", ", 4, ", ", 5, ", and ", 6]}
iex> Cldr.List.intersperse(["a"], MyApp.Cldr)
{:ok, ["a"]}
iex> Cldr.List.intersperse([1,2], MyApp.Cldr)
{:ok, [1, " and ", 2]}
"""
@spec intersperse(list(term()), Cldr.backend(), Keyword.t()) ::
{:ok, list(String.t())} | {:error, {module(), String.t()}}
def intersperse(list, backend \\ nil, options \\ [])
def intersperse(list, options, []) when is_list(options) do
{_locale, backend} = Cldr.locale_and_backend_from(options)
module = Module.concat(backend, List)
module.intersperse(list, options)
end
def intersperse(list, backend, options) do
{_locale, backend} = Cldr.locale_and_backend_from(options[:locale], backend)
module = Module.concat(backend, List)
module.intersperse(list, options)
end
@doc """
Formats a list using `intersperse/2` but raises if there is
an error.
## Examples
iex> Cldr.List.intersperse!(["a", "b", "c"], MyApp.Cldr, locale: "en")
["a", ", ", "b", ", and ", "c"]
iex> Cldr.List.intersperse!(["a", "b", "c"], MyApp.Cldr, locale: "en", format: :unit_narrow)
["a", " ", "b", " ", "c"]
"""
@spec intersperse!(list(term()), Cldr.backend(), Keyword.t()) :: list(String.t()) | no_return()
def intersperse!(list, backend \\ nil, options \\ []) do
{_locale, backend} = Cldr.locale_and_backend_from(options[:locale], backend)
module = Module.concat(backend, List)
module.intersperse!(list, options)
end
@doc """
Returns the list patterns for a locale.
List patterns provide rules for combining multiple
items into a language format appropriate for a locale.
## Example
iex> Cldr.List.list_patterns_for "en", MyApp.Cldr
%{
or: %{
"2": [0, " or ", 1],
end: [0, ", or ", 1],
middle: [0, ", ", 1],
start: [0, ", ", 1]
},
or_narrow: %{
"2": [0, " or ", 1],
end: [0, ", or ", 1],
middle: [0, ", ", 1],
start: [0, ", ", 1]
},
or_short: %{
"2": [0, " or ", 1],
end: [0, ", or ", 1],
middle: [0, ", ", 1],
start: [0, ", ", 1]
},
standard: %{
"2": [0, " and ", 1],
end: [0, ", and ", 1],
middle: [0, ", ", 1],
start: [0, ", ", 1]
},
standard_narrow: %{
"2": [0, ", ", 1],
end: [0, ", ", 1],
middle: [0, ", ", 1],
start: [0, ", ", 1]
},
standard_short: %{
"2": [0, " & ", 1],
end: [0, ", & ", 1],
middle: [0, ", ", 1],
start: [0, ", ", 1]
},
unit: %{
"2": [0, ", ", 1],
end: [0, ", ", 1],
middle: [0, ", ", 1],
start: [0, ", ", 1]
},
unit_narrow: %{
"2": [0, " ", 1],
end: [0, " ", 1],
middle: [0, " ", 1],
start: [0, " ", 1]
},
unit_short: %{
"2": [0, ", ", 1],
end: [0, ", ", 1],
middle: [0, ", ", 1],
start: [0, ", ", 1]
}
}
"""
def list_patterns_for(locale, backend \\ default_backend()) do
{locale, backend} = Cldr.locale_and_backend_from(locale, backend)
module = Module.concat(backend, List)
module.list_patterns_for(locale)
end
@doc """
Returns the formats of list patterns available for a locale.
Returns a list of `atom`s of of the list formats that are
available in CLDR for a locale.
## Example
iex> Cldr.List.list_formats_for("en", MyApp.Cldr)
[:or, :or_narrow, :or_short, :standard, :standard_narrow,
:standard_short, :unit, :unit_narrow, :unit_short]
"""
def list_formats_for(locale, backend \\ nil) do
{locale, backend} = Cldr.locale_and_backend_from(locale, backend)
module = Module.concat(backend, List)
module.list_formats_for(locale)
end
@deprecated "Use Cldr.List.list_formats_for/2"
defdelegate list_styles_for(locale, backend), to: __MODULE__, as: :list_formats_for
# TODO Remove at version 3.0
@doc false
defdelegate list_pattern_styles_for(locale, backend), to: __MODULE__, as: :list_formats_for
@doc """
Return the list of known list formats.
## Example
iex> Cldr.List.known_list_formats()
[:or, :or_narrow, :or_short, :standard, :standard_narrow,
:standard_short, :unit, :unit_narrow, :unit_short]
"""
@known_list_formats Cldr.Config.get_locale("root", %Cldr.Config{locales: ["root"]})
|> Map.get(:list_formats) |> Map.keys
def known_list_formats do
@known_list_formats
end
@deprecated "Use Cldr.List.known_list_formats/0"
def known_list_styles do
known_list_formats()
end
@doc false
# TODO remove for Cldr 3.0
if Code.ensure_loaded?(Cldr) && function_exported?(Cldr, :default_backend!, 0) do
def default_backend do
Cldr.default_backend!()
end
else
def default_backend do
Cldr.default_backend()
end
end
end