Skip to content

Commit

Permalink
[add] Main framework of Version 2 based on TS, Parcel & Jest
Browse files Browse the repository at this point in the history
  • Loading branch information
TechQuery committed Oct 1, 2019
0 parents commit 552eb73
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
package-lock.json
dist/
.cache/
54 changes: 54 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "cell-router",
"version": "2.0.0-alpha.0",
"license": "AGPL-3.0",
"description": "",
"keywords": [],
"author": "shiy2008@gmail.com",
"homepage": "https://github.com/EasyWebApp/cell-router#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/EasyWebApp/cell-router.git"
},
"bugs": {
"url": "https://github.com/EasyWebApp/cell-router/issues"
},
"main": "index.js",
"devDependencies": {
"@types/jest": "^24.0.18",
"@types/jsdom": "^12.2.4",
"@types/mocha": "^5.2.7",
"husky": "^3.0.7",
"jest": "^24.9.0",
"jsdom": "^15.1.1",
"lint-staged": "^9.4.1",
"parcel-bundler": "^1.12.3",
"prettier": "^1.18.2",
"ts-jest": "^24.1.0",
"typescript": "^3.6.3"
},
"scripts": {
"test": "lint-staged && jest",
"build": "parcel build source/index.ts"
},
"prettier": {
"singleQuote": true,
"tabWidth": 4
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node"
},
"lint-staged": {
"*.ts": [
"prettier --write",
"git add"
]
},
"husky": {
"hooks": {
"pre-commit": "npm test",
"pre-push": "npm run build"
}
}
}
30 changes: 30 additions & 0 deletions source/History.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export enum HistoryMode {
hash = '#',
path = '/'
}

const { location } = window;

export default class History {
readonly root = location.pathname;
mode = HistoryMode.hash;

constructor(mode?: HistoryMode) {
this.mode = mode || this.mode;
}

get path() {
return location.href.slice(
(location.origin + (this.root + this.mode).replace(/\/{2,}/g, '/'))
.length
);
}

push(path: string, title = document.title, data = {}) {
window.history.pushState(
data,
(document.title = title),
(this.root + this.mode + path).replace(/\/{2,}/g, '/')
);
}
}
1 change: 1 addition & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './History';
20 changes: 20 additions & 0 deletions test/History.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import './polyfill';
import History from '../source/History';

describe('History', () => {
var history: History;

it('should have correct properties after construction', () => {
history = new History();

expect(history.root).toBe('/');
expect(history.mode).toBe('#');
});

it('should change path & title of document after calling .goto()', () => {
history.push('/test', 'Test');

expect(history.path).toBe('/test');
expect(document.title).toBe('Test');
});
});
6 changes: 6 additions & 0 deletions test/polyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { JSDOM } from 'jsdom';

const { window } = new JSDOM('', { url: 'http://localhost/' });

// @ts-ignore
for (const name of ['window', 'document']) global[name] = window[name];
17 changes: 17 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"outDir": "dist/",
"sourceMap": true,
"strict": true,
"noImplicitReturns": true,
"noImplicitAny": true,
"module": "es6",
"moduleResolution": "node",
"esModuleInterop":true,
"target": "es5",
"allowJs": true
},
"include": [
"source/**/*"
]
}

0 comments on commit 552eb73

Please sign in to comment.