DEV Community

Lukas Gaucas
Lukas Gaucas

Posted on • Updated on

PHP-like namespaces in JavaScript using Unicode identifiers (2.1 ed.)

Using unicode "U+3035" i.e. "vertical kana" symbol that bears resemblance to \ i.e. backward slash we can produce something like this :

Example 1

〵App〵Console〵Logger = console.log
〵App〵Console〵Logger("Hello World") // "Hello World"
Enter fullscreen mode Exit fullscreen mode

Example 2

〵Window〵document = window.document // # ~ one-line
// # ~ multiple-line
〵WindowBase = window 
〵Document = document
〵WindowBase .〵Document // # PAY ATTENTION at dot (.) between "namespaces"
Enter fullscreen mode Exit fullscreen mode

User defined snippet

How to: write one of the prefixes in your IDE .js |.ts files e.g. kana and press tab (make sure "editor.tabCompletion": "on")

{
    // @https://code.visualstudio.com/docs/editor/userdefinedsnippets
    // Place your global snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and 
    // description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope 
    // is left empty or omitted, the snippet gets applied to all languages. The prefix is what is 
    // used to trigger the snippet and the body will be expanded and inserted. Possible variables are: 
    // $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. 
    // Placeholders with the same ids are connected.
    "namespace_seperator_javascript": {
        "scope": "javascript,typescript",
        "prefix": ["sep", "kana", "js-namespace", "ts-namespace"],
        "body": [
            "〵$0"
        ],
        "description": "JS, TS friendy unicode-based namespacing-like char"
    }
}
Enter fullscreen mode Exit fullscreen mode

Practical example – variadic function returning value of single type (filter_-ish_) function :

globalThis.kana = RegExp('\u{3035}').source // # '〵'
// function <name>〵<returning type>
function variadic〵string(...〵names){
    for (const name of 〵names){
        if (typeof name === variadic〵string.name.match("string$")[0]){
            console.log(name);
        }
    }
}
variadic〵string(true, "false", 1, 0, "ABC", {}) // # "false", "ABC"
Enter fullscreen mode Exit fullscreen mode

If you think it could be applicable, use it !

Cheers !

Top comments (0)

Visualizing Promises and Async/Await 🤯

async await

Learn the ins and outs of Promises and Async/Await!