@competitionlabs/gamification-widget-loader 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ajax.js +65 -0
- package/dist/loader.js +1 -0
- package/example/example.js +22 -0
- package/example/index.html +13 -0
- package/loader.js +182 -0
- package/package.json +19 -0
- package/sizeof.js +27 -0
- package/vite.config.js +12 -0
package/ajax.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export const createAjaxClass = function ({ sizeof }) {
|
|
2
|
+
return class Ajax {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.xhr = new XMLHttpRequest();
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/* eslint no-undef: "off" */
|
|
8
|
+
createCORSRequest(method, url) {
|
|
9
|
+
if ('withCredentials' in this.xhr) {
|
|
10
|
+
/* Most browsers. */ this.xhr.open(method, url, true);
|
|
11
|
+
} else if (typeof XDomainRequest !== 'undefined') {
|
|
12
|
+
/* IE8 & IE9 */ this.xhr = new XDomainRequest();
|
|
13
|
+
url = url.indexOf('https') > -1 && location.protocol !== 'https:' ? url.replace('https', 'http') : url;
|
|
14
|
+
this.xhr.open(method, url);
|
|
15
|
+
} else {
|
|
16
|
+
/* CORS not supported. */ this.xhr = null;
|
|
17
|
+
}
|
|
18
|
+
return this.xhr;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
abort() {
|
|
22
|
+
if (this.xhr && this.xhr.readyState !== undefined && this.xhr.readyState !== 4) {
|
|
23
|
+
this.xhr.abort();
|
|
24
|
+
}
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Retrieves data from a URL without a page refresh.
|
|
30
|
+
*/
|
|
31
|
+
getData(data) {
|
|
32
|
+
const obj = this;
|
|
33
|
+
try {
|
|
34
|
+
data.type = data.type !== undefined && typeof data.type === 'string' && data.type.length > 0 ? data.type : 'POST';
|
|
35
|
+
data.data = data.data !== undefined && typeof data.data === 'object' ? data.data : {};
|
|
36
|
+
data.url = data.url !== undefined && typeof data.url === 'string' && data.url.length > 0 ? data.url : '';
|
|
37
|
+
data.success = data.success !== undefined ? data.success : function () {};
|
|
38
|
+
data.error = data.error !== undefined ? data.error : function () {};
|
|
39
|
+
data.headers = data.headers !== undefined ? data.headers : {};
|
|
40
|
+
data.extraCallback = data.extraCallback !== undefined ? data.extraCallback : function () {};
|
|
41
|
+
obj.xhr = this.createCORSRequest(data.type, data.url);
|
|
42
|
+
obj.xhr.onload = function () {
|
|
43
|
+
data.extraCallback(data, obj.xhr);
|
|
44
|
+
data.success(obj.xhr.responseText, data, obj.xhr);
|
|
45
|
+
};
|
|
46
|
+
obj.xhr.onerror = function () {
|
|
47
|
+
data.error(obj.xhr.status);
|
|
48
|
+
};
|
|
49
|
+
if (typeof XDomainRequest === 'undefined') {
|
|
50
|
+
if (sizeof(data.headers) > 0) {
|
|
51
|
+
for (const item in data.headers) {
|
|
52
|
+
obj.xhr.setRequestHeader(item, data.headers[item]);
|
|
53
|
+
}
|
|
54
|
+
} else if ((data.type === 'POST' || data.type === 'PUT' || data.type === 'DELETE') && sizeof(data.headers) === 0) {
|
|
55
|
+
obj.xhr.setRequestHeader('Content-Type', 'application/json');
|
|
56
|
+
} else {
|
|
57
|
+
obj.xhr.setRequestHeader('Content-Type', 'text/plain');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
obj.xhr.send(JSON.stringify(data.data));
|
|
61
|
+
return obj.xhr;
|
|
62
|
+
} catch {}
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
};
|
package/dist/loader.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(){"use strict";const h=function(s){return typeof Element<"u"&&s instanceof Element},p=function(s){let a=0,t;for(t in s)s.hasOwnProperty(t)&&a++;return a===0&&h(s)&&(a=1),a},w=function({sizeof:s}){return class{constructor(){this.xhr=new XMLHttpRequest}createCORSRequest(t,e){return"withCredentials"in this.xhr?this.xhr.open(t,e,!0):typeof XDomainRequest<"u"?(this.xhr=new XDomainRequest,e=e.indexOf("https")>-1&&location.protocol!=="https:"?e.replace("https","http"):e,this.xhr.open(t,e)):this.xhr=null,this.xhr}abort(){return this.xhr&&this.xhr.readyState!==void 0&&this.xhr.readyState!==4&&this.xhr.abort(),this}getData(t){const e=this;try{if(t.type=t.type!==void 0&&typeof t.type=="string"&&t.type.length>0?t.type:"POST",t.data=t.data!==void 0&&typeof t.data=="object"?t.data:{},t.url=t.url!==void 0&&typeof t.url=="string"&&t.url.length>0?t.url:"",t.success=t.success!==void 0?t.success:function(){},t.error=t.error!==void 0?t.error:function(){},t.headers=t.headers!==void 0?t.headers:{},t.extraCallback=t.extraCallback!==void 0?t.extraCallback:function(){},e.xhr=this.createCORSRequest(t.type,t.url),e.xhr.onload=function(){t.extraCallback(t,e.xhr),t.success(e.xhr.responseText,t,e.xhr)},e.xhr.onerror=function(){t.error(e.xhr.status)},typeof XDomainRequest>"u")if(s(t.headers)>0)for(const i in t.headers)e.xhr.setRequestHeader(i,t.headers[i]);else(t.type==="POST"||t.type==="PUT"||t.type==="DELETE")&&s(t.headers)===0?e.xhr.setRequestHeader("Content-Type","application/json"):e.xhr.setRequestHeader("Content-Type","text/plain");return e.xhr.send(JSON.stringify(t.data)),e.xhr}catch{}}}};(function(){const s=w({sizeof:p});var a=function(){this.ajax=new s,this.settings={apiKey:"",spaceName:"my_space_name",enforceGameLookup:!0,language:"en",currency:"",enableNotifications:!1,memberId:"",uri:{gatewayDomain:"",gatewayWSDomain:"",translationPath:"",widgetData:"/api/v1/:space/widget"},uomSymbol:"",isWidgetMinimized:!0,bindContainer:null,miniBindContainer:null,loadTranslationsForceCustomPath:!0,widgetStatus:"Live",resources:[],assetsBasePath:"/build"},this.initialiseWidget=function(t){var f,g,d;const e=this;console.log("initialiseWidget",t);const i={...t.data[0],widgets:{type_1:{script:"https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/default/javascript/gamification-ux-package.v3-selfinit.js",css:["https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/default/css/styles.css"],translations:{ja:"https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/default/i18n/translation_ja.json",en:"https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/default/i18n/translation_en.json"}},type_2:{script:"https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/type_2/javascript/gamification-ux-package.v3-selfinit.js",css:["https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/type_2/css/styles.css"],translations:{ja:"https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/type_2/i18n/translation_ja.json",en:"https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/type_2/i18n/translation_en.json"}}}};console.log("MY WIDGET DATA",i);const c=i.layout.toLowerCase(),n=(f=i.widgets)==null?void 0:f[c];if(!n)throw new Error(`CompetitionLabs widget configuration does not contain assets for ${i.layout}`);const m=((g=n.translations)==null?void 0:g[e.settings.language])||((d=n.translations)==null?void 0:d.en)||"";(Array.isArray(n.css)?n.css:[]).forEach(function(o){if(!Array.from(document.head.querySelectorAll('link[rel="stylesheet"]')).some(r=>r.href===new URL(o,document.baseURI).href)){const r=document.createElement("link");r.rel="stylesheet",r.href=o,document.head.appendChild(r)}});const y={enableNotifications:e.settings.enableNotifications,memberId:e.settings.memberId,uri:{...e.settings.uri,translationPath:m},uomSymbol:e.settings.uomSymbol,spaceName:e.settings.spaceName,apiKey:e.settings.apiKey,isWidgetMinimized:e.settings.isWidgetMinimized,bindContainer:e.settings.bindContainer,miniBindContainer:e.settings.miniBindContainer,language:e.settings.language,loadTranslationsForceCustomPath:e.settings.loadTranslationsForceCustomPath,resources:[...e.settings.resources],widgetStatus:e.settings.widgetStatus,widgetData:Array.isArray(t==null?void 0:t.data)?t.data:[i]},u=function(){if(typeof window._clWidgetV32026SelfInit!="function")throw new Error("CompetitionLabs widget script did not create window._clWidgetV32026SelfInit");window._clWidgetV32026=new window._clWidgetV32026SelfInit({...y})};if(!n.script)throw new Error(`CompetitionLabs widget configuration does not contain a script for ${i.layout}`);if(typeof window._clWidgetV32026SelfInit<"u")u();else{const o=document.createElement("script");o.type="text/javascript",o.src=n.script,o.onload=function(){u()},o.onerror=function(){throw new Error(`CompetitionLabs widget script failed to load: ${n.script}`)},document.body.appendChild(o)}},this.getWidgetData=function(t){const e=this,i=`${e.settings.uri.gatewayDomain}${e.settings.uri.widgetData.replace(":space",e.settings.spaceName)}?status=${e.settings.widgetStatus}`;e.ajax.abort().getData({type:"GET",url:i,headers:{"X-API-KEY":e.settings.apiKey},success:function(c,n,m){if(m.status===200){const l=JSON.parse(c);e.initialiseWidget(l),typeof t=="function"&&t(l)}},error:function(){typeof t=="function"&&t()}})},this.init=function(t){const e=this;console.log("Initting Data"),e.getWidgetData(t)}};window.clLoaderV3=new a})()})();
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const loader = window.clLoaderV3;
|
|
2
|
+
loader.settings = {
|
|
3
|
+
...loader.settings,
|
|
4
|
+
apiKey: '37de27fd1d6242f283e826034bd83a81',
|
|
5
|
+
spaceName: 'client_2',
|
|
6
|
+
memberId: 'fynniqABiZWyLxn9MTLM',
|
|
7
|
+
uri: {
|
|
8
|
+
...loader.settings.uri,
|
|
9
|
+
gatewayDomain: 'http://192.168.91.30:9998',
|
|
10
|
+
gatewayWSDomain: 'ws://192.168.91.30:9998'
|
|
11
|
+
},
|
|
12
|
+
uomSymbol: '€',
|
|
13
|
+
bindContainer: document.querySelector('#widget'),
|
|
14
|
+
language: 'en',
|
|
15
|
+
widgetStatus: 'Live',
|
|
16
|
+
isWidgetMinimized: false,
|
|
17
|
+
resources: [
|
|
18
|
+
'https://fonts.googleapis.com/css2?family=Inter:wght@100..900&display=swap'
|
|
19
|
+
]
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
loader.init();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>CompetitionLabs Widget Loader Example</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div id="widget"></div>
|
|
10
|
+
<script type="module" src="../loader.js"></script>
|
|
11
|
+
<script type="module" src="/example/example.js"></script>
|
|
12
|
+
</body>
|
|
13
|
+
</html>
|
package/loader.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/*
|
|
2
|
+
COMPETITION LABS LTD v1.0.0
|
|
3
|
+
(c) 2026 Nathan Galea
|
|
4
|
+
License: www.competitionlabs.com/terms-of-service
|
|
5
|
+
|
|
6
|
+
Centralised loader script that is responsible for loading/handling all your product/environment specific scripts
|
|
7
|
+
in one place. Allows better control over your vast product range, centralising all your business related configurations
|
|
8
|
+
in one place:
|
|
9
|
+
- load all your scripts/styles/configurations/presets from one script
|
|
10
|
+
- control when and how the scripts get initialised
|
|
11
|
+
- restrict loading per product
|
|
12
|
+
*/
|
|
13
|
+
import sizeof from './sizeof.js';
|
|
14
|
+
import { createAjaxClass } from './ajax.js';
|
|
15
|
+
|
|
16
|
+
(function () {
|
|
17
|
+
'use strict';
|
|
18
|
+
|
|
19
|
+
const Ajax = createAjaxClass({ sizeof });
|
|
20
|
+
|
|
21
|
+
var loadScript = function () {
|
|
22
|
+
this.ajax = new Ajax();
|
|
23
|
+
this.settings = {
|
|
24
|
+
apiKey: '', // default API key
|
|
25
|
+
spaceName: 'my_space_name', // default space name
|
|
26
|
+
enforceGameLookup: true, // default enforce game lookup
|
|
27
|
+
language: 'en', // default language setting
|
|
28
|
+
currency: '', // default currency setting
|
|
29
|
+
enableNotifications: false, // requires a working/valid SSE channel
|
|
30
|
+
memberId: '',
|
|
31
|
+
uri: {
|
|
32
|
+
gatewayDomain: '',
|
|
33
|
+
gatewayWSDomain: '',
|
|
34
|
+
translationPath: '',
|
|
35
|
+
widgetData: '/api/v1/:space/widget'
|
|
36
|
+
},
|
|
37
|
+
uomSymbol: '',
|
|
38
|
+
isWidgetMinimized: true,
|
|
39
|
+
bindContainer: null,
|
|
40
|
+
miniBindContainer: null,
|
|
41
|
+
loadTranslationsForceCustomPath: true,
|
|
42
|
+
widgetStatus: 'Live',
|
|
43
|
+
resources: [],
|
|
44
|
+
assetsBasePath: '/build'
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
this.initialiseWidget = function (widgetData) {
|
|
48
|
+
const _this = this;
|
|
49
|
+
console.log('initialiseWidget', widgetData);
|
|
50
|
+
|
|
51
|
+
const widget = {
|
|
52
|
+
...widgetData.data[0],
|
|
53
|
+
widgets: {
|
|
54
|
+
type_1: {
|
|
55
|
+
script: "https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/default/javascript/gamification-ux-package.v3-selfinit.js",
|
|
56
|
+
css: ["https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/default/css/styles.css"],
|
|
57
|
+
translations: {
|
|
58
|
+
ja: "https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/default/i18n/translation_ja.json",
|
|
59
|
+
en: "https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/default/i18n/translation_en.json"
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
type_2: {
|
|
63
|
+
script: "https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/type_2/javascript/gamification-ux-package.v3-selfinit.js",
|
|
64
|
+
css: ["https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/type_2/css/styles.css"],
|
|
65
|
+
translations: {
|
|
66
|
+
ja: "https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/type_2/i18n/translation_ja.json",
|
|
67
|
+
en: "https://s3.eu-west-1.amazonaws.com/demo.gamify.mt/gamification-widget/competitionlabs-gamification-widget-v3/themes/type_2/i18n/translation_en.json"
|
|
68
|
+
}
|
|
69
|
+
},
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
console.log('MY WIDGET DATA', widget)
|
|
74
|
+
const widgetType = widget.layout.toLowerCase();
|
|
75
|
+
const widgetAssets = widget.widgets?.[widgetType];
|
|
76
|
+
|
|
77
|
+
if (!widgetAssets) {
|
|
78
|
+
throw new Error(`CompetitionLabs widget configuration does not contain assets for ${widget.layout}`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const translationPath = widgetAssets.translations?.[_this.settings.language] || widgetAssets.translations?.en || '';
|
|
82
|
+
const stylesheets = Array.isArray(widgetAssets.css) ? widgetAssets.css : [];
|
|
83
|
+
|
|
84
|
+
stylesheets.forEach(function (stylesheet) {
|
|
85
|
+
const isAlreadyLoaded = Array.from(document.head.querySelectorAll('link[rel="stylesheet"]'))
|
|
86
|
+
.some((link) => link.href === new URL(stylesheet, document.baseURI).href);
|
|
87
|
+
|
|
88
|
+
if (!isAlreadyLoaded) {
|
|
89
|
+
const link = document.createElement('link');
|
|
90
|
+
link.rel = 'stylesheet';
|
|
91
|
+
link.href = stylesheet;
|
|
92
|
+
document.head.appendChild(link);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const widgetInstanceData = {
|
|
97
|
+
enableNotifications: _this.settings.enableNotifications,
|
|
98
|
+
memberId: _this.settings.memberId,
|
|
99
|
+
uri: {
|
|
100
|
+
..._this.settings.uri,
|
|
101
|
+
translationPath
|
|
102
|
+
},
|
|
103
|
+
uomSymbol: _this.settings.uomSymbol,
|
|
104
|
+
spaceName: _this.settings.spaceName,
|
|
105
|
+
apiKey: _this.settings.apiKey,
|
|
106
|
+
isWidgetMinimized: _this.settings.isWidgetMinimized,
|
|
107
|
+
bindContainer: _this.settings.bindContainer,
|
|
108
|
+
miniBindContainer: _this.settings.miniBindContainer,
|
|
109
|
+
language: _this.settings.language,
|
|
110
|
+
loadTranslationsForceCustomPath: _this.settings.loadTranslationsForceCustomPath,
|
|
111
|
+
resources: [..._this.settings.resources],
|
|
112
|
+
widgetStatus: _this.settings.widgetStatus,
|
|
113
|
+
widgetData: Array.isArray(widgetData?.data) ? widgetData.data : [widget]
|
|
114
|
+
};
|
|
115
|
+
const initialise = function () {
|
|
116
|
+
if (typeof window._clWidgetV32026SelfInit !== 'function') {
|
|
117
|
+
throw new Error('CompetitionLabs widget script did not create window._clWidgetV32026SelfInit');
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
window._clWidgetV32026 = new window._clWidgetV32026SelfInit({
|
|
121
|
+
...widgetInstanceData
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
if (!widgetAssets.script) {
|
|
126
|
+
throw new Error(`CompetitionLabs widget configuration does not contain a script for ${widget.layout}`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (typeof window._clWidgetV32026SelfInit !== 'undefined') {
|
|
130
|
+
initialise();
|
|
131
|
+
|
|
132
|
+
}else {
|
|
133
|
+
const script = document.createElement('script');
|
|
134
|
+
script.type = 'text/javascript';
|
|
135
|
+
script.src = widgetAssets.script;
|
|
136
|
+
script.onload = function () {
|
|
137
|
+
initialise();
|
|
138
|
+
};
|
|
139
|
+
script.onerror = function () {
|
|
140
|
+
throw new Error(`CompetitionLabs widget script failed to load: ${widgetAssets.script}`);
|
|
141
|
+
};
|
|
142
|
+
document.body.appendChild(script);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
this.getWidgetData = function (callback) {
|
|
148
|
+
const _this = this;
|
|
149
|
+
const widgetDataUrl = `${_this.settings.uri.gatewayDomain}${_this.settings.uri.widgetData.replace(':space', _this.settings.spaceName)}?status=${_this.settings.widgetStatus}`;
|
|
150
|
+
_this.ajax.abort().getData({
|
|
151
|
+
type: 'GET',
|
|
152
|
+
url: widgetDataUrl,
|
|
153
|
+
headers: {
|
|
154
|
+
'X-API-KEY': _this.settings.apiKey
|
|
155
|
+
},
|
|
156
|
+
success: function (response, dataObj, xhr) {
|
|
157
|
+
if (xhr.status === 200) {
|
|
158
|
+
const json = JSON.parse(response);
|
|
159
|
+
_this.initialiseWidget(json);
|
|
160
|
+
if (typeof callback === 'function') {
|
|
161
|
+
callback(json);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
error: function () {
|
|
166
|
+
if (typeof callback === 'function') {
|
|
167
|
+
callback();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
this.init = function (callback) {
|
|
174
|
+
const _this = this;
|
|
175
|
+
console.log('Initting Data')
|
|
176
|
+
_this.getWidgetData(callback);
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
/* eslint new-cap: "off" */
|
|
181
|
+
window.clLoaderV3 = new loadScript();
|
|
182
|
+
})();
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@competitionlabs/gamification-widget-loader",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/loader.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"dev": "vite --open /example/index.html",
|
|
9
|
+
"build": "vite build",
|
|
10
|
+
"publish": "npm run build && npm publish --access restricted",
|
|
11
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"vite": "^6.1.0"
|
|
18
|
+
}
|
|
19
|
+
}
|
package/sizeof.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const isElement = function (value) {
|
|
2
|
+
return typeof Element !== 'undefined' && value instanceof Element;
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* returns the size of an Object or array
|
|
7
|
+
*
|
|
8
|
+
* @memberOf module:utils
|
|
9
|
+
* @static
|
|
10
|
+
* @param obj {Object}
|
|
11
|
+
* @return {Number}
|
|
12
|
+
*/
|
|
13
|
+
const sizeof = function (obj) {
|
|
14
|
+
let size = 0;
|
|
15
|
+
let key;
|
|
16
|
+
for (key in obj) {
|
|
17
|
+
if (obj.hasOwnProperty(key)) size++;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (size === 0 && isElement(obj)) {
|
|
21
|
+
size = 1;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return size;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export default sizeof;
|