Skip to content

Commit

Permalink
Merge pull request #99 from scssyworks/feature/ver4
Browse files Browse the repository at this point in the history
Feature/ver4
  • Loading branch information
scssyworks authored Sep 9, 2022
2 parents 541b66b + 01314ca commit 4b1a259
Show file tree
Hide file tree
Showing 19 changed files with 2,227 additions and 2,353 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"extends": "eslint:recommended",
"rules": {
"no-console": 1
"no-console": 1,
"no-undef": 0
}
}
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"endOfLine": "auto"
}
4,355 changes: 2,150 additions & 2,205 deletions dist/render/silkrouter.iife.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/render/silkrouter.iife.js.map

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 11 additions & 19 deletions src/js/api/bindRouterEvents/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
import { fromEvent } from 'rxjs';
import { POP_STATE, QRY, VIRTUAL_PUSHSTATE } from '../../utils/constants';
import { POP_STATE, VIRTUAL_PUSHSTATE } from '../../utils/constants';
import { trigger } from '../../utils/triggerEvent';
import collate from '../collate';
import { trim } from '../../utils/utils';
import { getGlobal } from '../../utils/getGlobal';
import { getPath } from '../../utils/getPath';

export default function bindRouterEvents() {
const { context, location, hashRouting } = this.config;
this.popStateSubscription = fromEvent(getGlobal(), POP_STATE).subscribe(
export default function bindRouterEvents(inst) {
const { context, location, hashRouting: hash } = inst.config;
inst.popStateSubscription = fromEvent(getGlobal(), POP_STATE).subscribe(
(e) => {
const path = trim(
hashRouting
? location.hash.substring(1).split(QRY)[0]
: location.pathname
);
const path = getPath(hash, location);
if (path) {
trigger(context, VIRTUAL_PUSHSTATE, [
{ path, hash: hashRouting },
e,
this,
]);
trigger(context, VIRTUAL_PUSHSTATE, [{ path, hash }, e, inst]);
}
}
);
this.listeners = fromEvent(context, VIRTUAL_PUSHSTATE).pipe(
collate.apply(this)
inst.listeners = fromEvent(context, VIRTUAL_PUSHSTATE).pipe(
collate.apply(inst)
);
if (hashRouting && !location.hash) {
this.set('/', true, false); // Replace current hash path without executing anythings
if (hash && !location.hash) {
inst.set('/', true, false); // Replace current hash path without executing anythings
}
}
8 changes: 3 additions & 5 deletions src/js/api/callOnce/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { Observable } from 'rxjs';
import { QRY, UNDEF } from '../../utils/constants';
import { trim } from '../../utils/utils';
import { UNDEF } from '../../utils/constants';
import { getPath } from '../../utils/getPath';
import RouterEvent from '../routerEvent';

export default function callOnce(isDone) {
const { hashRouting: hash, location, init } = this.config;
const path = trim(
hash ? location.hash.substring(1).split(QRY)[0] : location.pathname
);
const path = getPath(hash, location);
return (observable) =>
new Observable((subscriber) => {
const subn = observable.subscribe(subscriber);
Expand Down
6 changes: 3 additions & 3 deletions src/js/api/resolveQuery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { QRY } from '../../utils/constants';
export default function resolveQuery(queryString, hashRouting) {
const { location } = this.config;
const search = trim(location.search && location.search.substring(1));
const existingQuery = hashRouting
? trim(location.hash.split(QRY)[1])
: trim(search);
const existingQuery = trim(
hashRouting ? location.hash.split(QRY)[1] : search
);
if (!existingQuery) return queryString;
return toQueryString(
assign(deparam(search), deparam(existingQuery), deparam(queryString))
Expand Down
4 changes: 2 additions & 2 deletions src/js/api/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ export default class Router {
)
);
this.__paths__ = [];
bindRouterEvents.apply(this);
bindRouterEvents(this);
}
pipe(...ops) {
return this.listeners.pipe(callOnce.apply(this), ...ops);
}
subscribe(...fns) {
return this.listeners.pipe(callOnce.apply(this)).subscribe(...fns);
return this.pipe().subscribe(...fns);
}
set(...props) {
return set.apply(this, props);
Expand Down
5 changes: 2 additions & 3 deletions src/js/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import './utils/polyfills';
import Router from './api/router';
import * as operators from './operators';
import Router from "./api/router";
import * as operators from "./operators";

export { Router, operators };
25 changes: 2 additions & 23 deletions src/js/utils/assign.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,8 @@
import { isObject } from "deparam.js";
import { each } from "./utils";

/**
* Inner loop function for assign
* @private
* @param {object} ref Argument object
* @param {object} target First object
*/
function loopFunc(ref, target) {
if (isObject(ref)) {
each(ref, (prop, key) => {
target[key] = prop;
});
}
}

/**
* Polyfill for Object.assign only smaller and with less features
* Function to extend an object with new and updated properties
* @private
* @returns {object}
*/
export function assign(...args) {
const target = isObject(args[0]) ? args[0] : {};
each(args, (arg) => {
loopFunc(arg, target);
});
return target;
return Object.assign(...args);
}
3 changes: 1 addition & 2 deletions src/js/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export const REG_COMPLEX = /\[/;
export const REG_VARIABLE = /([^[]+)\[([^[]*)\]/;
export const REG_REPLACE_BRACKETS = /\[([^[]*)\]/;
export const REG_REPLACE_NEXTPROP = /[^[]+/;
export const HISTORY_UNSUPPORTED =
'Current browser does not support history object';
export const HISTORY_UNSUPPORTED = 'History unsupported!';
export const INVALID_ROUTE = 'Route string is not a pure route';
export const VIRTUAL_PUSHSTATE = 'vpushstate';
export const CACHED_FIELDS = [
Expand Down
8 changes: 8 additions & 0 deletions src/js/utils/getPath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { QRY } from './constants';
import { trim } from './utils';

export const getPath = (isHash, location) => {
return trim(
isHash ? location.hash.substring(1).split(QRY)[0] : location.pathname
);
};
37 changes: 0 additions & 37 deletions src/js/utils/polyfills.js

This file was deleted.

10 changes: 6 additions & 4 deletions src/js/utils/query.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { isObject } from "deparam.js";
import { AMP, EMPTY, TYPEOF_FUNC, TYPEOF_STR } from "./constants";
import { each, isArr } from "./utils";
import { isObject } from 'deparam.js';
import { AMP, EMPTY, TYPEOF_FUNC, TYPEOF_STR } from './constants';
import { each, isArr } from './utils';

export const encode = encodeURIComponent;

/**
* Builds query string recursively
Expand All @@ -15,7 +17,7 @@ function buildQuery(qsList, key, obj) {
buildQuery(qsList, `${key}[${isArr(obj) ? EMPTY : obKey}]`, prop);
});
} else if (typeof obj !== TYPEOF_FUNC) {
qsList.push(`${encodeURIComponent(key)}=${encodeURIComponent(obj)}`);
qsList.push(`${encode(key)}=${encode(obj)}`);
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/js/utils/triggerEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { TYPEOF_STR } from './constants';
import { getGlobal } from './getGlobal';
import { each } from './utils';

const g = getGlobal();

/**
* Function to trigger custom event
* @param {Node|NodeList|HTMLCollection|Node[]} target Target element or list
Expand All @@ -14,7 +12,7 @@ export function trigger(target, eventType, data) {
target = Array.from(target instanceof Node ? [target] : target);
if (target.length && typeof eventType === TYPEOF_STR) {
each(target, (el) => {
const customEvent = new g.CustomEvent(eventType, {
const customEvent = new getGlobal().CustomEvent(eventType, {
bubbles: true,
cancelable: true,
detail: data || [],
Expand Down
6 changes: 3 additions & 3 deletions src/js/utils/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isNumber, isObject } from "deparam.js";
import { EMPTY, REG_PATHNAME, TYPEOF_BOOL, TYPEOF_STR } from "./constants";
import { isNumber, isObject } from 'deparam.js';
import { EMPTY, REG_PATHNAME, TYPEOF_BOOL, TYPEOF_STR } from './constants';

/**
* Shorthand for Array.isArray
Expand All @@ -25,7 +25,7 @@ export function trim(str) {
* @param {string} route Route string
*/
export function isValidRoute(route) {
return typeof route === TYPEOF_STR && REG_PATHNAME.test(route);
return REG_PATHNAME.test(route);
}

/**
Expand Down
42 changes: 0 additions & 42 deletions tests/assign.test.js

This file was deleted.

25 changes: 25 additions & 0 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { isValidRoute, trim } from '../src/js/utils/utils';

describe('Utils', () => {
describe('trim', () => {
it('should trim text', () => {
expect(trim(' test ')).toBe('test');
});
it('should return empty string if input value is invalid', () => {
expect(trim(null)).toBe('');
});
});

describe('isValidRoute', () => {
it('should return true of route is valid', () => {
expect(isValidRoute('/test/path')).toBeTruthy();
});
it('should return false of route is INVALID', () => {
expect(isValidRoute('test/path')).toBeFalsy();
});
it('should return false of route is UNDEFINED or NULL', () => {
expect(isValidRoute()).toBeFalsy();
expect(isValidRoute(null)).toBeFalsy();
});
});
});

0 comments on commit 4b1a259

Please sign in to comment.