Best JavaScript code snippet using wpt
Namespace.js
Source:Namespace.js
1/**2 * Constructs a new Namespace.3 * @exports ProtoBuf.Reflect.Namespace4 * @param {!ProtoBuf.Builder} builder Builder reference5 * @param {?ProtoBuf.Reflect.Namespace} parent Namespace parent6 * @param {string} name Namespace name7 * @param {Object.<string,*>=} options Namespace options8 * @param {string?} syntax The syntax level of this definition (e.g., proto3)9 * @constructor10 * @extends ProtoBuf.Reflect.T11 */12var Namespace = function(builder, parent, name, options, syntax) {13 T.call(this, builder, parent, name);14 /**15 * @override16 */17 this.className = "Namespace";18 /**19 * Children inside the namespace.20 * @type {!Array.<ProtoBuf.Reflect.T>}21 */22 this.children = [];23 /**24 * Options.25 * @type {!Object.<string, *>}26 */27 this.options = options || {};28 /**29 * Syntax level (e.g., proto2 or proto3).30 * @type {!string}31 */32 this.syntax = syntax || "proto2";33};34/**35 * @alias ProtoBuf.Reflect.Namespace.prototype36 * @inner37 */38var NamespacePrototype = Namespace.prototype = Object.create(T.prototype);39/**40 * Returns an array of the namespace's children.41 * @param {ProtoBuf.Reflect.T=} type Filter type (returns instances of this type only). Defaults to null (all children).42 * @return {Array.<ProtoBuf.Reflect.T>}43 * @expose44 */45NamespacePrototype.getChildren = function(type) {46 type = type || null;47 if (type == null)48 return this.children.slice();49 var children = [];50 for (var i=0, k=this.children.length; i<k; ++i)51 if (this.children[i] instanceof type)52 children.push(this.children[i]);53 return children;54};55/**56 * Adds a child to the namespace.57 * @param {ProtoBuf.Reflect.T} child Child58 * @throws {Error} If the child cannot be added (duplicate)59 * @expose60 */61NamespacePrototype.addChild = function(child) {62 var other;63 if (other = this.getChild(child.name)) {64 // Try to revert camelcase transformation on collision65 if (other instanceof Message.Field && other.name !== other.originalName && this.getChild(other.originalName) === null)66 other.name = other.originalName; // Revert previous first (effectively keeps both originals)67 else if (child instanceof Message.Field && child.name !== child.originalName && this.getChild(child.originalName) === null)68 child.name = child.originalName;69 else70 throw Error("Duplicate name in namespace "+this.toString(true)+": "+child.name);71 }72 this.children.push(child);73};74/**75 * Gets a child by its name or id.76 * @param {string|number} nameOrId Child name or id77 * @return {?ProtoBuf.Reflect.T} The child or null if not found78 * @expose79 */80NamespacePrototype.getChild = function(nameOrId) {81 var key = typeof nameOrId === 'number' ? 'id' : 'name';82 for (var i=0, k=this.children.length; i<k; ++i)83 if (this.children[i][key] === nameOrId)84 return this.children[i];85 return null;86};87/**88 * Resolves a reflect object inside of this namespace.89 * @param {string|!Array.<string>} qn Qualified name to resolve90 * @param {boolean=} excludeNonNamespace Excludes non-namespace types, defaults to `false`91 * @return {?ProtoBuf.Reflect.Namespace} The resolved type or null if not found92 * @expose93 */94NamespacePrototype.resolve = function(qn, excludeNonNamespace) {95 var part = typeof qn === 'string' ? qn.split(".") : qn,96 ptr = this,97 i = 0;98 if (part[i] === "") { // Fully qualified name, e.g. ".My.Message'99 while (ptr.parent !== null)100 ptr = ptr.parent;101 i++;102 }103 var child;104 do {105 do {106 if (!(ptr instanceof Reflect.Namespace)) {107 ptr = null;108 break;109 }110 child = ptr.getChild(part[i]);111 if (!child || !(child instanceof Reflect.T) || (excludeNonNamespace && !(child instanceof Reflect.Namespace))) {112 ptr = null;113 break;114 }115 ptr = child; i++;116 } while (i < part.length);117 if (ptr != null)118 break; // Found119 // Else search the parent120 if (this.parent !== null)121 return this.parent.resolve(qn, excludeNonNamespace);122 } while (ptr != null);123 return ptr;124};125/**126 * Determines the shortest qualified name of the specified type, if any, relative to this namespace.127 * @param {!ProtoBuf.Reflect.T} t Reflection type128 * @returns {string} The shortest qualified name or, if there is none, the fqn129 * @expose130 */131NamespacePrototype.qn = function(t) {132 var part = [], ptr = t;133 do {134 part.unshift(ptr.name);135 ptr = ptr.parent;136 } while (ptr !== null);137 for (var len=1; len <= part.length; len++) {138 var qn = part.slice(part.length-len);139 if (t === this.resolve(qn, t instanceof Reflect.Namespace))140 return qn.join(".");141 }142 return t.fqn();143};144/**145 * Builds the namespace and returns the runtime counterpart.146 * @return {Object.<string,Function|Object>} Runtime namespace147 * @expose148 */149NamespacePrototype.build = function() {150 /** @dict */151 var ns = {};152 var children = this.children;153 for (var i=0, k=children.length, child; i<k; ++i) {154 child = children[i];155 if (child instanceof Namespace)156 ns[child.name] = child.build();157 }158 if (Object.defineProperty)159 Object.defineProperty(ns, "$options", { "value": this.buildOpt() });160 return ns;161};162/**163 * Builds the namespace's '$options' property.164 * @return {Object.<string,*>}165 */166NamespacePrototype.buildOpt = function() {167 var opt = {},168 keys = Object.keys(this.options);169 for (var i=0, k=keys.length; i<k; ++i) {170 var key = keys[i],171 val = this.options[keys[i]];172 // TODO: Options are not resolved, yet.173 // if (val instanceof Namespace) {174 // opt[key] = val.build();175 // } else {176 opt[key] = val;177 // }178 }179 return opt;180};181/**182 * Gets the value assigned to the option with the specified name.183 * @param {string=} name Returns the option value if specified, otherwise all options are returned.184 * @return {*|Object.<string,*>}null} Option value or NULL if there is no such option185 */186NamespacePrototype.getOption = function(name) {187 if (typeof name === 'undefined')188 return this.options;189 return typeof this.options[name] !== 'undefined' ? this.options[name] : null;...
stkwrap.js
Source:stkwrap.js
1; (function () {2 const yawf = window.yawf;3 const util = yawf.util;4 const stk = yawf.stk = {};5 const strings = util.strings;6 const randStr = strings.randKey();7 const key = `yawf_stk_${randStr}`;8 util.inject(function (key) {9 if (window.STK) return;10 const wrappers = [];11 const wrapRegister = function (register) {12 return function (name, registerFunction, scope) {13 const original = registerFunction;14 let wrapped = original;15 if (name === 'namespace') {16 wrapped = wrapNamespace(registerFunction);17 } else {18 wrappers.forEach(wrapper => {19 if (name === wrapper.name) {20 wrapped = wrapper.wrapper(registerFunction);21 }22 });23 }24 return register.call(this, name, wrapped, scope);25 };26 };27 const wrapNamespace = function (namespaceFunctionGetter) {28 return function () {29 const namespaceFunction = namespaceFunctionGetter.apply(this, arguments);30 const fakeNamespaceKey = 'yawf_proto_getter';31 const namespaceInstance = namespaceFunction(fakeNamespaceKey);32 const namespacePrototype = namespaceInstance.constructor.prototype;33 namespacePrototype.register = wrapRegister(namespacePrototype.register);34 delete namespacePrototype.namespace[fakeNamespaceKey];35 return namespaceFunction;36 };37 };38 let stk = null;39 try {40 Object.defineProperty(window, 'STK', {41 get() { return stk; },42 set(trueStk) {43 trueStk.register = wrapRegister(trueStk.register);44 stk = trueStk;45 },46 enumerable: true,47 });48 } catch (e) {49 // ignore50 }51 Object.defineProperty(window, key, {52 get() { return void 0; },53 set({ name, wrapper }) {54 wrappers.push({ name, wrapper });55 },56 enumerable: false,57 });58 }, key);59 let stkInfoResolve = null;60 stk.info = new Promise(resolve => { stkInfoResolve = resolve; });61 const initInfoKey = 'yawf_init_info' + strings.randKey();62 util.inject(function (key, name, ...params) {63 window[key] = {64 name,65 wrapper: (function (initInfoKey) {66 const gotInfo = function (info) {67 const event = new CustomEvent(initInfoKey, {68 detail: { info: JSON.stringify(info) },69 });70 window.dispatchEvent(event);71 };72 return function (regFunc) {73 return function (stk) {74 const inner = regFunc.call(this, stk);75 return function (plc_top, info, ...params) {76 gotInfo(info);77 return inner(plc_top, info, ...params);78 };79 };80 };81 }(...params)),82 };83 }, key, 'pl.top.source.init', initInfoKey);84 window.addEventListener(initInfoKey, function gotInitInfo(event) {85 event.stopPropagation();86 if (!event.detail.info) return;87 const info = JSON.parse(event.detail.info);88 stkInfoResolve(Object.freeze(info));89 window.removeEventListener(initInfoKey, gotInitInfo);90 }, true);...
namespace.ts
Source:namespace.ts
1type NamespacePrototype = {2 iri: string;3 prefix: string;4 terms: readonly string[];5};6type NamespacePrefix<Namespace extends NamespacePrototype> =7 Namespace["prefix"];8type NamespaceIri<Namespace extends NamespacePrototype> = Namespace["iri"];9type NamespaceObject<Namespace extends NamespacePrototype> = {10 [Term in Namespace["terms"][number]]: `${NamespacePrefix<Namespace>}${Term}`;11};12export const createNamespace = <13 N extends NamespacePrototype,14 I = NamespaceIri<N>,15 P = NamespacePrefix<N>,16 O = NamespaceObject<N>17>(18 namespaceSpec: N19) =>20 Object.assign(21 //<X extends I>(f: [X]) =>22 // `${namespaceSpec.prefix}:${f}` as `${string & P}${string & X}`,23 namespaceSpec.terms.reduce((acc, term) => {24 //acc[term] = `${namespaceSpec.prefix}${term}`25 acc[term] = `${namespaceSpec.iri}${term}`;26 return acc;27 }, {} as any),28 {29 $prefix: namespaceSpec["prefix"],30 $iri: namespaceSpec["iri"],31 }32 ) as O & {33 $prefix: P;34 $iri: I;...
Using AI Code Generation
1wpt.namespacePrototype("com.wpt", "Person", {2 getName: function() {3 return this.name;4 },5 getAge: function() {6 return this.age;7 }8});9wpt.namespace("com.wpt");10com.wpt.Person = function() {11 this.name = "John";12 this.age = 30;13 this.getName = function() {14 return this.name;15 };16 this.getAge = function() {17 return this.age;18 };19};20wpt.namespace("com.wpt");21com.wpt.Person = function() {22 this.name = "John";23 this.age = 30;24};25com.wpt.Person.prototype = {26 getName: function() {27 return this.name;28 },29 getAge: function() {30 return this.age;31 }32};33wpt.namespace("com.wpt");34com.wpt.Person = function() {35 this.name = "John";36 this.age = 30;37};38com.wpt.Person.prototype.getName = function() {39 return this.name;40};41com.wpt.Person.prototype.getAge = function() {42 return this.age;43};44wpt.namespace("com.wpt");45com.wpt.Person = function() {46 this.name = "John";47 this.age = 30;48};49com.wpt.Person.prototype.getName = function() {50 return this.name;51};52com.wpt.Person.prototype.getAge = function() {53 return this.age;54};55com.wpt.Person.prototype = {56};57wpt.namespace("com.wpt");58com.wpt.Person = function() {59 this.name = "John";60 this.age = 30;61};62com.wpt.Person.prototype.getName = function() {63 return this.name;64};65com.wpt.Person.prototype.getAge = function() {66 return this.age;67};68com.wpt.Person.prototype.constructor = com.wpt.Person;69wpt.namespace("com.wpt");70com.wpt.Person = function() {71 this.name = "John";72 this.age = 30;73};74com.wpt.Person.prototype = {
Using AI Code Generation
1wpt.namespacePrototype('wpt', 'namespacePrototype', function (namespace, method, fn) {2 namespace = namespace.split('.');3 var root = window;4 for (var i = 0; i < namespace.length; i++) {5 root = root[namespace[i]] = root[namespace[i]] || {};6 }7 root[method] = fn;8});9wpt.namespacePrototype('wpt', 'namespacePrototype', function (namespace, method, fn) {10 namespace = namespace.split('.');11 var root = window;12 for (var i = 0; i < namespace.length; i++) {13 root = root[namespace[i]] = root[namespace[i]] || {};14 }15 root[method] = fn;16});17wpt.namespacePrototype('wpt', 'namespacePrototype', function (namespace, method, fn) {18 namespace = namespace.split('.');19 var root = window;20 for (var i = 0; i < namespace.length; i++) {21 root = root[namespace[i]] = root[namespace[i]] || {};22 }23 root[method] = fn;24});25wpt.namespacePrototype('wpt', 'namespacePrototype', function (namespace, method, fn) {26 namespace = namespace.split('.');27 var root = window;28 for (var i = 0; i < namespace.length; i++) {29 root = root[namespace[i]] = root[namespace[i]] || {};30 }31 root[method] = fn;32});
Using AI Code Generation
1var wptools = require('wptools');2var texas = wptools.namespacePrototype('Texas');3console.log(texas);4var wptools = require('wptools');5var texas = wptools.page('Texas');6console.log(texas);7var wptools = require('wptools');8var texas = wptools.page('Texas', 'en');9console.log(texas);10var wptools = require('wptools');11var texas = wptools.page('Texas', 'en', 'html');12console.log(texas);13var wptools = require('wptools');14var texas = wptools.page('Texas', 'en', 'html', 'wikipedia');15console.log(texas);16var wptools = require('wptools');17console.log(texas);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!