<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>namespace.js - awaken the monster in you; javascript library by schukai GmbH</title> <meta name="description" content="javascript library for optimal use on fast and flexible pages." /> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc.css"> <script src="scripts/nav.js" defer></script> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <input type="checkbox" id="nav-trigger" class="nav-trigger" /> <label for="nav-trigger" class="navicon-button x"> <div class="navicon"></div> </label> <label for="nav-trigger" class="overlay"></label> <nav > <input type="text" id="nav-search" placeholder="Search" /> <h2><a href="index.html">Home</a></h2><h2><a href="https://www.schukai.com/" target="_blank" class="menu-item" id="schukai_link" >schukai GmbH</a></h2><h2><a href="https://www.npmjs.com/package/@schukai/monster" target="_blank" class="menu-item" id="npm_link" >NPM</a></h2><h3>Namespaces</h3><ul><li><a href="global.html#Monster">Monster</a><ul class='methods'><li data-type='method' style='display: none;'><a href="global.html#Monster#.assignToNamespace">assignToNamespace</a></li><li data-type='method' style='display: none;'><a href="global.html#Monster#.getVersion">getVersion</a></li></ul></li><li><a href="Monster_Math.html">Monster/Math</a><ul class='methods'><li data-type='method' style='display: none;'><a href="Monster_Math.html#.random">random</a></li></ul></li><li><a href="Monster_Types.html">Monster/Types</a><ul class='methods'><li data-type='method' style='display: none;'><a href="Monster_Types.html#.isArray">isArray</a></li><li data-type='method' style='display: none;'><a href="Monster_Types.html#.isBoolean">isBoolean</a></li><li data-type='method' style='display: none;'><a href="Monster_Types.html#.isFunction">isFunction</a></li><li data-type='method' style='display: none;'><a href="Monster_Types.html#.isObject">isObject</a></li><li data-type='method' style='display: none;'><a href="Monster_Types.html#.isPrimitive">isPrimitive</a></li><li data-type='method' style='display: none;'><a href="Monster_Types.html#.isString">isString</a></li><li data-type='method' style='display: none;'><a href="Monster_Types.html#.validateArray">validateArray</a></li><li data-type='method' style='display: none;'><a href="Monster_Types.html#.validateBoolean">validateBoolean</a></li><li data-type='method' style='display: none;'><a href="Monster_Types.html#.validateFunction">validateFunction</a></li><li data-type='method' style='display: none;'><a href="Monster_Types.html#.validateObject">validateObject</a></li><li data-type='method' style='display: none;'><a href="Monster_Types.html#.validatePrimitive">validatePrimitive</a></li><li data-type='method' style='display: none;'><a href="Monster_Types.html#.validateString">validateString</a></li></ul></li><li><a href="Monster_Util.html">Monster/Util</a><ul class='methods'><li data-type='method' style='display: none;'><a href="Monster_Util.html#.clone">clone</a></li><li data-type='method' style='display: none;'><a href="Monster_Util.html#.deepFreeze">deepFreeze</a></li></ul></li></ul><h3>Classes</h3><ul><li><a href="Monster.Namespace.html">Namespace</a><ul class='methods'><li data-type='method' style='display: none;'><a href="Monster.Namespace.html#getNamespace">getNamespace</a></li><li data-type='method' style='display: none;'><a href="Monster.Namespace.html#toString">toString</a></li></ul></li><li><a href="Monster_Types.ID.html">ID</a></li><li><a href="Monster_Types.Object.html">Object</a><ul class='methods'><li data-type='method' style='display: none;'><a href="Monster_Types.Object.html#toString">toString</a></li></ul></li><li><a href="Monster_Types.Observer.html">Observer</a></li><li><a href="Monster_Types.ObserverList.html">ObserverList</a></li><li><a href="Monster_Types.Version.html">Version</a><ul class='methods'><li data-type='method' style='display: none;'><a href="Monster_Types.Version.html#compareTo">compareTo</a></li><li data-type='method' style='display: none;'><a href="Monster_Types.Version.html#toString">toString</a></li></ul></li></ul><h3><a href="global.html">Global</a></h3> </nav> <div id="main"> <h1 class="page-title">namespace.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>'use strict'; /** * @namespace Monster * @author schukai GmbH */ /** * Namespace class objects form the basic framework of the namespace administration. * * All functions, classes and objects of the library hang within the namespace tree. * * Via `obj instanceof Monster.Namespace` it is also easy to check whether it is an object or a namespace. * * @memberOf Monster * @copyright schukai GmbH * @since 1.0.0 */ class Namespace { /** * * @param namespace * @param obj */ constructor(namespace) { if (namespace === undefined || typeof namespace !== 'string') { throw new Error("namespace is not a string") } this.namespace = namespace; } /** * * @returns {string} */ getNamespace() { return this.namespace; } /** * * @returns {string} */ toString() { return this.getNamespace(); } } /** * * @type {Namespace} * @global */ export const Monster = new Namespace("Monster"); /** * */ assignToNamespace('Monster', assignToNamespace); /** * To expand monster, the `Monster.assignToNamespace()` method can be used. * * you must call the method in the monster namespace. this allows you to mount your own classes, objects and functions into the namespace. * * To avoid confusion and so that you do not accidentally overwrite existing functions, you should use the custom namespace `X`. * * ``` * <script type="module"> * import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.0.14/dist/modules/namespace.js'; * function hello() { * console.log('Hello World!'); * } * Monster.assignToNamespace("Monster.X",hello) * Monster.X.hello(); // ↦ Hello World! * </script> * * ``` * * @param ns * @param obj * @memberOf Monster */ function assignToNamespace(ns, ...obj) { let current = namespaceFor(ns.split(".")); for (let i = 0, l = obj.length; i < l; i++) { current[objectName(obj[i])] = obj[i]; } } /** * * @param fn * @returns {string|*} * @private */ function objectName(fn) { try { if (typeof fn !== 'function') { throw new Error("the first argument is not a function or class."); } if (fn.hasOwnProperty('name')) { return fn.name; } if ("function" === typeof fn.toString) { let s = fn.toString(); let f = s.match(/^\s*function\s+([^\s(]+)/); if (Array.isArray(f) && typeof f[1] === 'string') { return f[1]; } let c = s.match(/^\s*class\s+([^\s(]+)/); if (Array.isArray(c) && typeof c[1] === 'string') { return c[1]; } } } catch (e) { throw new Error("exception " + e); } throw new Error("the name of the class or function cannot be resolved."); } /** * * @param parts * @returns {Namespace} * @private */ function namespaceFor(parts) { var space = Monster, ns = 'Monster'; for (let i = 0; i < parts.length; i++) { if ("Monster" === parts[i]) { continue; } ns += '.' + parts[i]; if (!space.hasOwnProperty(parts[i])) { space[parts[i]] = new Namespace(ns); } space = space[parts[i]]; } return space; } export {assignToNamespace} </code></pre> </article> </section> </div> <br class="clear"> <footer> </footer> <script>prettyPrint();</script> <script src="scripts/polyfill.js"></script> <script src="scripts/linenumber.js"></script> <script src="scripts/search.js" defer></script> <script src="scripts/collapse.js" defer></script> </body> </html>