-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[add] Main framework of Version 2 based on TS, Parcel & Jest
- Loading branch information
0 parents
commit 552eb73
Showing
7 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules/ | ||
package-lock.json | ||
dist/ | ||
.cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, '/') | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './History'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/**/*" | ||
] | ||
} |