JSFiddle - React, Tailwind, and code Playground

by John Schulz

JavaScript

console.log(ns('foo.bar'), foo);
console.log(ns('some.arbitrary.depth.string'), some);
console.log(ns('some.other.ns'), some);

function ns( str, root )
{
    var parts = str.split('.'),
        part, ndx = -1,
        len = parts.length;

    root = root || this;

    while (++ndx < len) {
        part = parts[ndx];
        if (!(part in root)) root[part] = {};
        root = root[part];
    }

    return root;
}