summaryrefslogtreecommitdiff
path: root/node_modules/bcp-47/lib/stringify.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/bcp-47/lib/stringify.js')
-rw-r--r--node_modules/bcp-47/lib/stringify.js50
1 files changed, 50 insertions, 0 deletions
diff --git a/node_modules/bcp-47/lib/stringify.js b/node_modules/bcp-47/lib/stringify.js
new file mode 100644
index 0000000..e5b367a
--- /dev/null
+++ b/node_modules/bcp-47/lib/stringify.js
@@ -0,0 +1,50 @@
+/**
+ * @typedef {Partial<import('./parse.js').Schema>} Schema
+ * @typedef {Partial<import('./parse.js').Extension>} Extension
+ */
+
+/**
+ * Compile a language schema to a BCP 47 language tag.
+ *
+ * @param {Schema} schema
+ * @returns {string}
+ */
+export function stringify(schema = {}) {
+ /** @type {Array<string>} */
+ let result = []
+
+ if (schema.irregular) {
+ return schema.irregular
+ }
+
+ if (schema.regular) {
+ return schema.regular
+ }
+
+ if (schema.language) {
+ result = result.concat(
+ schema.language,
+ schema.extendedLanguageSubtags || [],
+ schema.script || [],
+ schema.region || [],
+ schema.variants || []
+ )
+
+ const values = schema.extensions || []
+ let index = -1
+
+ while (++index < values.length) {
+ const value = values[index]
+
+ if (value.singleton && value.extensions && value.extensions.length > 0) {
+ result.push(value.singleton, ...value.extensions)
+ }
+ }
+ }
+
+ if (schema.privateuse && schema.privateuse.length > 0) {
+ result.push('x', ...schema.privateuse)
+ }
+
+ return result.join('-')
+}