white-label-router 1.1.2 → 4.0.0

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/package.json CHANGED
@@ -1,11 +1,15 @@
1
1
  {
2
2
  "name": "white-label-router",
3
- "version": "1.1.2",
3
+ "version": "4.0.0",
4
4
  "description": "A simple ES6 JS router",
5
5
  "main": "dist/index.js",
6
+ "files": [
7
+ "dist",
8
+ "README.md"
9
+ ],
6
10
  "engines": {
7
- "node": ">=4.0",
8
- "npm": ">=2.0"
11
+ "node": "^22.18.0 || >=24.11.0",
12
+ "npm": ">=10.0"
9
13
  },
10
14
  "repository": {
11
15
  "type": "git",
@@ -32,12 +36,42 @@
32
36
  "url": "https://github.com/bshack/white-label-router/issues"
33
37
  },
34
38
  "homepage": "https://github.com/bshack/white-label-router#readme",
35
- "dependencies": {
36
- "gator": "x.x.x"
39
+ "scripts": {
40
+ "build": "tsc -p tsconfig.json",
41
+ "test": "npm run build && npm run test:types && npm run test:unit",
42
+ "audit": "npm audit --audit-level=high",
43
+ "typecheck": "tsc -p tsconfig.json --noEmit",
44
+ "test:unit": "node --test test/*.test.js",
45
+ "coverage": "npm run build && npm run test:types && c8 npm run test:unit",
46
+ "prepack": "npm run build",
47
+ "test:types": "tsc -p tsconfig.consumer.json"
37
48
  },
38
49
  "devDependencies": {
39
- "babel-cli": "^6.6.4",
40
- "babel-plugin-transform-es2015-modules-umd": "^6.6.0",
41
- "babel-preset-es2015": "^6.6.0"
50
+ "@types/node": "24.13.3",
51
+ "c8": "12.0.0",
52
+ "typescript": "7.0.2"
53
+ },
54
+ "types": "dist/index.d.ts",
55
+ "c8": {
56
+ "all": true,
57
+ "include": [
58
+ "src/**/*.ts"
59
+ ],
60
+ "reporter": [
61
+ "text",
62
+ "json",
63
+ "json-summary",
64
+ "lcov"
65
+ ],
66
+ "excludeAfterRemap": true,
67
+ "checkCoverage": true,
68
+ "lines": 100,
69
+ "functions": 100,
70
+ "branches": 100,
71
+ "statements": 100,
72
+ "perFile": true,
73
+ "exclude": [
74
+ "**/*.d.ts"
75
+ ]
42
76
  }
43
77
  }
package/.babelrc DELETED
@@ -1,4 +0,0 @@
1
- {
2
- "presets": ["es2015"],
3
- "plugins": ["transform-es2015-modules-umd"]
4
- }
package/.editorconfig DELETED
@@ -1,13 +0,0 @@
1
- # EditorConfig is awesome: http://EditorConfig.org
2
-
3
- root = true
4
-
5
- [*]
6
- end_of_line = lf
7
- insert_final_newline = true
8
- charset = utf-8
9
- indent_style = space
10
- indent_size = 4
11
- max_line_length = 120
12
- quote_type = single
13
- indent_brace_style = K&R
package/.npmignore DELETED
@@ -1,2 +0,0 @@
1
- node_modules/
2
- npm-debug.log
package/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- language: node_js
2
- node_js:
3
- - 6
4
- - 5
5
- - 4
package/bin/compile.sh DELETED
@@ -1,2 +0,0 @@
1
- #!/bin/bash
2
- ../node_modules/babel-cli/bin/babel.js ../src/index.js --out-file ../dist/index.js
package/bower.json DELETED
@@ -1,26 +0,0 @@
1
- {
2
- "name": "white-label-router",
3
- "description": "A simple ES6 JS router.",
4
- "main": "dist/index.js",
5
- "authors": [
6
- "bshack"
7
- ],
8
- "license": "MIT",
9
- "keywords": [
10
- "javascript",
11
- "js",
12
- "router",
13
- "es6",
14
- "es2015",
15
- "delegate"
16
- ],
17
- "homepage": "https://github.com/bshack/white-label-router",
18
- "moduleType": [
19
- "amd"
20
- ],
21
- "ignore": [
22
- "**/.*",
23
- "node_modules"
24
- ],
25
- "version": "1.1.2"
26
- }
package/src/index.js DELETED
@@ -1,325 +0,0 @@
1
-
2
- // event delegation lib
3
- import Gator from 'gator';
4
-
5
- (() => {
6
-
7
- 'use strict';
8
-
9
- module.exports = class {
10
-
11
- constructor() {
12
-
13
- // placeholder for routes defined when extended
14
- this.routes = {
15
- defaultRoute: {
16
- view: {
17
- initialize: () => {
18
- return true;
19
- },
20
- destroy: () => {
21
- return true;
22
- }
23
- }
24
- }
25
- }
26
-
27
- // this will hold the current url
28
- this.url = '';
29
-
30
- // this holds the current route
31
- this.route = null;
32
-
33
- // can house a container for the router to act upon
34
- this.scope = null;
35
-
36
- // this holds the previous route for destorying later
37
- this.previousRoute = null;
38
-
39
- // this will hold the page title (not yet supported by browsers)
40
- this.pageTitle = null;
41
-
42
- // optional mediator placeholder
43
- this.mediator = false;
44
-
45
- }
46
-
47
- initialize() {
48
-
49
- // set orgin if not supported by the browser
50
- if (!window.location.origin) {
51
- window.location.origin = window.location.protocol + '//' + window.location.hostname +
52
- (window.location.port ? ':' + window.location.port: '');
53
- }
54
-
55
- //get the url fragment w/query string
56
- this.url = (window.location.pathname + (window.location.search || '') || '');
57
-
58
- // set up the events
59
- this.addListeners();
60
-
61
- // navigate to correct view
62
- this.navigate(false, {}, false);
63
-
64
- return this;
65
-
66
- }
67
-
68
- addListeners() {
69
-
70
- //bind all pushstate links
71
- Gator(document).on('click', 'a[href][data-pushstate]', this.eventPushStateClick.bind(this));
72
-
73
- //bind window popstates
74
- window.addEventListener('popstate', this.eventPopState.bind(this));
75
-
76
- //listen to a mediator if present
77
- if (this.mediator) {
78
-
79
- this.mediator.on('router:navigate', (data) => {
80
- this.navigate(data.url, (data || {}), false);
81
- });
82
-
83
- }
84
-
85
- return this;
86
-
87
- }
88
-
89
- destroy() {
90
-
91
- this.removeListeners();
92
-
93
- return this;
94
-
95
- }
96
-
97
- removeListeners() {
98
-
99
- //unbind all pushstate links
100
- Gator(document).off('click', 'a[href][data-pushstate]');
101
-
102
- //bind window popstates
103
- window.removeEventListener('popstate', this.eventPopState.bind(this));
104
-
105
- return this;
106
-
107
- }
108
-
109
- eventPushStateClick(e) {
110
-
111
- e.preventDefault();
112
-
113
- //for example an image tag can initiate a click event in side an anchor
114
- if (e.target.tagName === 'A') {
115
- this.url = e.target.getAttribute('href') || '';
116
- } else {
117
- this.url = e.target.parentNode.getAttribute('href') || '';
118
- }
119
-
120
- this.navigate(false, {}, false);
121
-
122
- return this;
123
-
124
- }
125
-
126
- eventPopState(e) {
127
-
128
- this.url = (e.state || '');
129
-
130
- this.navigate(false, {}, true);
131
-
132
- return this;
133
-
134
- }
135
-
136
- parseQueryString(queryString) {
137
-
138
- let params = {};
139
- let queries;
140
- let temp;
141
- let i;
142
- let l;
143
-
144
- // Split into key/value pairs
145
- queries = queryString.split('&');
146
-
147
- // Convert the array of strings into an object
148
- for (i = 0, l = queries.length; i < l; i++) {
149
- temp = queries[i].split('=');
150
- params[temp[0]] = temp[1];
151
- }
152
-
153
- return params;
154
-
155
- }
156
-
157
- setLocationData(mediatorData) {
158
-
159
- //split the url here to seperate query strings from url path
160
- let urlFragments = this.url.split('?');
161
-
162
- // this is the object passed to the matching view
163
- this.locationData = {
164
- url: this.url,
165
- data: {
166
- url: [],
167
- mediator: mediatorData,
168
- query: {}
169
- }
170
- };
171
-
172
- // parse out the url data
173
- this.locationData.data.url = urlFragments[0].replace(this.route, '').split('/');
174
- this.locationData.data.url.splice(0, 1);
175
-
176
- // remove any trailing empty items from the array
177
- if (this.locationData.data.url.length
178
- && this.locationData.data.url[this.locationData.data.url.length - 1] === '') {
179
- this.locationData.data.url.splice(this.locationData.data.url.length - 1, 1);
180
- }
181
-
182
- //add query string data
183
- if (urlFragments[1]) {
184
- this.locationData.data.query = this.parseQueryString(urlFragments[1].replace('?', ''));
185
- }
186
-
187
- }
188
-
189
- navigate(url, mediatorData, isPopState) {
190
-
191
- // allow navigate to use a specified url
192
- if (url) {
193
- this.url = url;
194
- }
195
-
196
- //clean up url for matching
197
- this.url = this.url.replace(window.location.origin, '');
198
-
199
- // reset this to null for new location
200
- this.route = null;
201
-
202
- //find the first matcher that starts with the same string as a defined route
203
- for (let route in this.routes) {
204
-
205
- // we have a match
206
- if (this.url.lastIndexOf(route, 0) === 0) {
207
-
208
- this.route = route;
209
-
210
- //stop looping we are done
211
- break;
212
-
213
- }
214
-
215
- }
216
-
217
- this.setLocationData(mediatorData);
218
-
219
- // if a catch all default route is defined execute that when not match is found
220
- if (!this.route && this.routes.defaultRoute) {
221
-
222
- this.route = 'defaultRoute';
223
-
224
- }
225
-
226
- if (this.route) {
227
-
228
- // check if the page is secure and if the user passes the specified secure checking function
229
- if (
230
- typeof this.routes[this.route].secure === 'function' &&
231
- this.routes[this.route].secure(this.scope, this.locationData) !== true
232
- ) {
233
- return false;
234
- }
235
-
236
- //if previous route then destroy it
237
- if (this.previousRoute) {
238
-
239
- // else if it is an object check if there is a view class
240
- if (
241
- typeof this.routes[this.previousRoute] === 'object' &&
242
- typeof this.routes[this.previousRoute].view === 'object' &&
243
- typeof this.routes[this.previousRoute].view.destroy === 'function'
244
- ) {
245
-
246
- //destroy the view
247
- this.routes[this.previousRoute].view.destroy(this.scope, this.locationData);
248
-
249
- }
250
-
251
- }
252
-
253
- //execute the route if it is a simple function
254
- if (typeof this.routes[this.route] === 'function') {
255
-
256
- //execute the view
257
- this.routes[this.route](this.scope, this.locationData);
258
-
259
- //cache the old route
260
- this.previousRoute = this.route;
261
-
262
- } else if (
263
- typeof this.routes[this.route] === 'object' &&
264
- typeof this.routes[this.route].view === 'function'
265
- ) {
266
-
267
- //execute the view
268
- this.routes[this.route].view(this.scope, this.locationData);
269
-
270
- // if a title is defined for this route we'll set it
271
- if (typeof this.routes[this.route].title === 'string') {
272
- this.pageTitle = this.routes[this.route].title;
273
- } else {
274
- this.pageTitle = null;
275
- }
276
-
277
- //cache the old route
278
- this.previousRoute = this.route;
279
-
280
- // else if it is an object check if there is a view class
281
- } else if (
282
- typeof this.routes[this.route] === 'object' &&
283
- typeof this.routes[this.route].view === 'object' &&
284
- typeof this.routes[this.route].view.initialize === 'function'
285
- ) {
286
-
287
- // if a title is defined for this route we'll set it
288
- if (typeof this.routes[this.route].title === 'string') {
289
- this.pageTitle = this.routes[this.route].title;
290
- } else {
291
- this.pageTitle = null;
292
- }
293
-
294
- //execute the view
295
- this.routes[this.route].view.initialize(this.scope, this.locationData);
296
-
297
- //cache the old route
298
- this.previousRoute = this.route;
299
-
300
- } else {
301
-
302
- return false;
303
-
304
- }
305
-
306
- }
307
-
308
- // make sure to not set pushstate on back button click
309
- if (!isPopState) {
310
- //set in history the new url
311
- window.history.pushState(this.url, this.pageTitle, this.url);
312
- }
313
-
314
- // since browsers don't support setting the title with pushstate yet
315
- if (this.pageTitle) {
316
- document.title = this.pageTitle;
317
- }
318
-
319
- return this;
320
-
321
- }
322
-
323
- };
324
-
325
- })();