about summary refs log tree commit diff stats
path: root/src/js/intl.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/intl.nim')
-rw-r--r--src/js/intl.nim46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/js/intl.nim b/src/js/intl.nim
new file mode 100644
index 00000000..28cc42d5
--- /dev/null
+++ b/src/js/intl.nim
@@ -0,0 +1,46 @@
+# Very minimal Intl module... TODO make it more complete
+
+import bindings/quickjs
+import js/javascript
+
+type
+  NumberFormat = ref object
+
+#TODO ...yeah
+proc newNumberFormat(name: string = "en-US",
+    options = none(JSObject)): NumberFormat {.jsctor.} =
+  return NumberFormat()
+
+#TODO: this should accept string/BigInt too
+proc format(nf: NumberFormat, num: float64): string {.jsfunc.} =
+  let s = $num
+  var i = 0
+  var L = s.len
+  for k in countdown(s.high, 0):
+    if s[k] == '.':
+      L = k
+      break
+  if L mod 3 != 0:
+    while i < L mod 3:
+      result &= s[i]
+      inc i
+    if i < L:
+      result &= ','
+  let j = i
+  while i < L:
+    if j != i and i mod 3 == j:
+      result &= ','
+    result &= s[i]
+    inc i
+  if i + 1 < s.len and s[i] == '.':
+    if not (s[i + 1] == '0' and s.len == i + 2):
+      while i < s.len:
+        result &= s[i]
+        inc i
+
+proc addIntlModule*(ctx: JSContext) =
+  let global = JS_GetGlobalObject(ctx)
+  let intl = JS_NewObject(ctx)
+  ctx.registerType(NumberFormat, namespace = intl)
+  ctx.defineProperty(global, "Intl", intl)
+  JS_FreeValue(ctx, global)