src/nodejs/jscore

    Dark Mode
Search:
Group by:

  • Core, primitives, basic proc, string basics, for JavaScript.

Example:

import src/nodejs/jscore
import sugar  # =>
doAssert base64encode("Como siempre: lo urgente no deja tiempo para lo importante".cstring) == "Q29tbyBzaWVtcHJlOiBsbyB1cmdlbnRlIG5vIGRlamEgdGllbXBvIHBhcmEgbG8gaW1wb3J0YW50ZQ==".cstring
doAssert base64decode("Q29tbyBzaWVtcHJlOiBsbyB1cmdlbnRlIG5vIGRlamEgdGllbXBvIHBhcmEgbG8gaW1wb3J0YW50ZQ==".cstring) == "Como siempre: lo urgente no deja tiempo para lo importante".cstring
doAssert deduplicate([9, 1, 2, 3, 4, 9, 9, 9, 0]) == @[9, 1, 2, 3, 4, 0]
doAssert deduplicate(@[9, 9, 9, 9]) == @[9]

for okis in ["y".cstring, "Y", "1",  "ON", "On", "oN", "on", "yes", "YES",
    "YEs", "YeS", "Yes", "yES", "yEs", "yeS", "TRUE", "TRUe", "TRuE", "TRue",
    "TrUE", "TrUe", "TruE", "True", "tRUE", "tRUe", "tRuE", "tRue", "trUE",
    "trUe", "truE", "true"]:
    doAssert parseBool(okis)

for nope in ["n".cstring, "N", "0", "NO", "No", "nO", "no", "OFF", "OFf",
  "OfF", "Off", "oFF", "oFf", "ofF", "off", "FALSE", "FALSe", "FALsE",
  "FALse", "FAlSE", "FAlSe", "FAlsE", "FAlse", "FaLSE", "FaLSe", "FaLsE",
  "FaLse", "FalSE", "FalSe", "FalsE", "False", "fALSE", "fALSe", "fALsE",
  "fALse", "fAlSE", "fAlSe", "fAlsE", "fAlse", "faLSE", "faLSe", "faLsE",
  "faLse", "falSE", "falSe", "falsE", "false"]:
  doAssert not parseBool(nope)

let exampl = "hello"
doAssert exampl[0] == 'h'
doAssert exampl[0 .. 3] == "hell".cstring
doAssert exampl[0 .. ^2] == "hell".cstring

Types

ArrayBuffer = ref object of JsRoot
  byteLength*: cint
Uint8Array = ref object of JsRoot
  BYTES_PER_ELEMENT*: cint

Procs

func `$`(self: Uint8Array or ArrayBuffer): string
func `&`(a, b: cstring): cstring {.importjs: "(# + #)", ...raises: [], tags: [].}
func `&`(a: char; b: cstring): cstring {.
    importjs: "(String.fromCharCode(#) + #)", ...raises: [], tags: [].}
func `&`(a: cstring; b: char): cstring {.
    importjs: "(# + String.fromCharCode(#))", ...raises: [], tags: [].}
func `&&=`(lhs, rhs: auto): any {.importjs: "(# &&= #)", discardable,
                                  ...raises: [], tags: [].}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_AND_assignment
func `??=`(lhs, rhs: auto): any {.importjs: "(# ??= #)", discardable,
                                  ...raises: [], tags: [].}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_nullish_assignment
proc `[]`(s: cstring; slice: HSlice[SomeInteger, BackwardsIndex]): cstring {.
    asmNoStackFrame.}
proc `[]`(s: cstring; slice: HSlice[SomeInteger, SomeInteger]): cstring {.
    asmNoStackFrame.}
func `[]`(self: Uint8Array or ArrayBuffer; index: Natural): uint8 {.
    importjs: "#[#]", ...raises: [], tags: [].}
func `[]=`(self: Uint8Array or ArrayBuffer; index: Natural; value: uint8) {.
    importjs: "#[#] = #", ...raises: [], tags: [].}
func base64decode(strng: cstring; encoding = "utf-8".cstring): cstring {.
    importjs: "Buffer.from(#, \'base64\').toString(#)", ...raises: [], tags: [].}
Convenience func to Base64 decode a string.
func base64encode(strng: cstring; encoding = "utf-8".cstring): cstring {.
    importjs: "Buffer.from(#, #).toString(\'base64\')", ...raises: [], tags: [].}
Convenience func to Base64 encode a string.
func capitalizeAscii(s: cstring): cstring {.importjs: """  (() => { const s = #; return s.charAt(0).toUpperCase() + s.slice(1) })()""",
    ...raises: [], tags: [].}
func contains(a, b: cstring): bool {.importjs: "(#.indexOf(#) >= 0)",
                                     ...raises: [], tags: [].}
func contains(a: cstring; b: char): bool {.
    importjs: "(#.indexOf(String.fromCharCode(#)) >= 0)", ...raises: [], tags: [].}
func deduplicate[T](arrai: openArray[T]): seq[T] {.importjs: "[...new Set(#)]",
    ...raises: [], tags: [].}
Convenience func to Deduplicate an array.
func find(s: cstring; ss: char): int {.importjs: "#.indexOf(String.fromCharCode(#))",
                                       ...raises: [], tags: [].}
func find(s: cstring; ss: cstring): int {.importjs: "#.indexOf(#)", ...raises: [],
    tags: [].}
func find(strng: cstring; regex: RegExp): cint {.importjs: "#.search(#)",
    ...raises: [], tags: [].}
func indentation(s: cstring): cint {.importjs: """  (() => {
    const m = #.match(/^[\s\\t]*/gm);
    let result = m[0].length;
    for (var i = 1; i < m.length; i++) { result = Math.min(m[i].length, result) }
    return result;
  })()""",
                                     ...raises: [], tags: [].}
Returns the amount of indentation all lines of s have in common, ignoring lines that consist only of whitespace.
func isDigit(c: char): bool {.importjs: "(() => { const c = #; return (c >= \'0\' && c <= \'9\') })()",
                              ...raises: [], tags: [].}
func isFinite(n: SomeNumber): bool {.importjs: "Number.$1(#)", ...raises: [],
                                     tags: [].}
func isInteger(n: SomeNumber): bool {.importjs: "Number.$1(#)", ...raises: [],
                                      tags: [].}
func isSafeInteger(n: SomeNumber): bool {.importjs: "Number.$1(#)", ...raises: [],
    tags: [].}
func len(self: Uint8Array): int {.importjs: "(#.length)", ...raises: [], tags: [].}
func match(strng: cstring; regex: RegExp): seq[cstring] {.
    importjs: "#.match(#)", ...raises: [], tags: [].}
func matchAll(strng: cstring; regex: RegExp): seq[cstring] {.
    importjs: "Array.from(#.matchAll(#))", ...raises: [], tags: [].}
func newArrayBuffer(number: Natural): ArrayBuffer {.
    importjs: "new ArrayBuffer(#)", ...raises: [], tags: [].}
func newUint8Array(number: Natural): Uint8Array {.importjs: "new Uint8Array(#)",
    ...raises: [], tags: [].}
func normalize(strng: cstring; form = "NFC".cstring): cstring {.
    importjs: "#.normalize(#)", ...raises: [], tags: [].}
func parseBool(s: cstring): bool {.asmNoStackFrame, ...raises: [], tags: [].}
Convenience func mimics Nim parseBool but optimized for NodeJS. Does NOT ignore '_', if you need to ignore '_' use stdlib or remove the '_'. The reason is that it is more strict than stdlib, because does not allow '_', is not the same as stdlib one.
proc parseFloat(s: cstring): BiggestFloat {.importjs: "parseFloat(#)",
    ...raises: [], tags: [].}
proc parseInt(s: char): cint {.importjs: "parseInt(String.fromCharCode(#), 10)",
                               ...raises: [], tags: [].}
proc parseInt(s: cstring): cint {.importjs: "parseInt(#, 10)", ...raises: [],
                                  tags: [].}
proc parseUInt(s: char): uint {.importjs: "parseInt(String.fromCharCode(#), 10)",
                                ...raises: [], tags: [].}
proc parseUInt(s: cstring): uint {.importjs: "parseInt(#, 10)", ...raises: [],
                                   tags: [].}
func repeat(s: cstring; n: Natural): cstring {.importjs: "#.repeat(#)",
    ...raises: [], tags: [].}
func replace(s, sub: cstring; by = "".cstring): cstring {.
    importjs: "#.replace(#, #)", ...raises: [], tags: [].}
func replace(s: cstring; sub: char; by = "".cstring): cstring {.
    importjs: "#.replace(String.fromCharCode(#), #)", ...raises: [], tags: [].}
func replace(s: cstring; sub: char; by: char): cstring {.
    importjs: "#.replace(String.fromCharCode(#), String.fromCharCode(#))",
    ...raises: [], tags: [].}
func replace(strng: cstring; regex: RegExp; by = "".cstring): cstring {.
    importjs: "#.replace(#, #)", ...raises: [], tags: [].}
func replaceAll(strng: cstring; regex: RegExp; by = "".cstring): cstring {.
    importjs: "#.replaceAll(#, #)", ...raises: [], tags: [].}
func shuffle(arrai: openArray[auto]): seq[auto] {.
    importjs: "#.sort(() => { return Math.random() - 0.5})", ...raises: [],
    tags: [].}
Convenience func to Random shuffle an array.
func slice(s: cstring; start: cint; ends: cint): cstring {.
    importjs: "#.slice(#, #)", ...raises: [], tags: [].}
func split(a: cstring; b: char): seq[cstring] {.
    importjs: "#.split(String.fromCharCode(#))", ...raises: [], tags: [].}
func split(a: cstring; b: cstring): seq[cstring] {.importjs: "#.split(#)",
    ...raises: [], tags: [].}
func split(strng: cstring; regex: RegExp): seq[cstring] {.
    importjs: "#.split(#)", ...raises: [], tags: [].}
func strip(s: cstring): cstring {.importjs: "#.trim()", ...raises: [], tags: [].}
func strip(s: cstring; leading: bool; trailing: bool): cstring {.importjs: """  (() => {
    let result = #;
    if (#) { result = result.trimStart() }
    if (#) { result = result.trimEnd()   }
    return result;
  })()""",
    ...raises: [], tags: [].}
func structuredClone(value, transfer: auto) {.importjs: "$1(#, #)", ...raises: [],
    tags: [].}
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
func structuredClone(value: auto) {.importjs: "$1(#)", ...raises: [], tags: [].}
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
func toArray(self: ArrayBuffer): seq[int] {.
    importjs: "Array.from(new Uint8Array(#))", ...raises: [], tags: [].}
func toArray(self: Uint8Array): seq[uint8] {.importjs: "Array.from(#)",
    ...raises: [], tags: [].}
func toCstring(self: Uint8Array or ArrayBuffer): cstring {.
    importjs: "JSON.stringify(#)", ...raises: [], tags: [].}
func toExponential(n: SomeFloat; fractionDigits: Positive): cstring {.
    importjs: "#.$1(#)", ...raises: [], tags: [].}
func toFixed(n: SomeFloat; digits: 0 .. 20): cstring {.importjs: "#.$1(#)",
    ...raises: [], tags: [].}
func toLowerAscii(c: char): cstring {.importjs: "String.fromCharCode(#).toLowerCase()",
                                      ...raises: [], tags: [].}
func toLowerAscii(s: cstring): cstring {.importjs: "#.toLowerCase()",
    ...raises: [], tags: [].}
func toPrecision(n: SomeFloat; fractionDigits: Positive): cstring {.
    importjs: "#.$1(#)", ...raises: [], tags: [].}
func toUpperAscii(c: char): cstring {.importjs: "String.fromCharCode(#).toUpperCase()",
                                      ...raises: [], tags: [].}
func toUpperAscii(s: cstring): cstring {.importjs: "#.toUpperCase()",
    ...raises: [], tags: [].}
func `||=`(lhs, rhs: auto): any {.importjs: "(# ||= #)", discardable,
                                  ...raises: [], tags: [].}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR_assignment