Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add HashPersist #68

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ try {
}
```

### Used in Hash
Basic Persist supports only url excluding hash. If you want to use hash, use the code below.
```js
import { HashPersist, registerHashPersist } from "@egjs/persist";

// Register globally to use HashPersist.
registerHashPersist();


// Use HashPersist
const persist = new HashPersist();

// set
persist.set("a", "A");

// get
persist.get("a");
```

### Used in SPA

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"types": "./index.d.ts",
"sideEffects": false,
"scripts": {
"start": "webpack-dev-server --open",
"start": "rollup -c -w",
"build": "rollup -c",
"build:webpack": "webpack --env production",
"test": "karma start",
Expand Down
57 changes: 57 additions & 0 deletions src/HashPersist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-disable class-methods-use-this */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All files are missing the license notification, could you please add it?

/* eslint-disable no-use-before-define */
import {location} from "./browser";
import {CONST_HASH} from "./consts";
import Persist from "./Persist";

/**
* Get or store the current state of the web page using JSON according to hash.
* @ko 웹 페이지의 현재 상태를 hash에 따라 JSON 형식으로 저장하거나 읽는다.
* @memberof eg.Persist
* @alias eg.Persist.HashPersist
* @extends eg.Persist
*
* @support {"ie": "9+", "ch" : "latest", "ff" : "latest", "sf" : "latest" , "edge" : "latest", "ios" : "7+", "an" : "2.3+ (except 3.x)"}
*/
class HashPersist extends Persist {
/**
* Read value
* @param {String?} path target path
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether this was intentional, but shouldn't this be just string type? (required?)

* @return {String|Number|Boolean|Object|Array}
*/
get(path) {
return this._get(this._getKey(), this._getPath(path));
}
/**
* Save value
* @param {String} path target path
* @param {String|Number|Boolean|Object|Array} value value to save
* @return {Persist}
*/
set(path, value) {
return this._set(this._getKey(), this._getPath(path), value);
}
/**
* Remove value
* @param {String} path target path
* @return {Persist}
*/
remove(path) {
return this._remove(this._getKey(), this._getPath(path));
}
_getKey() {
return `${CONST_HASH}${location.hash}`;
}
_getPath(path) {
let nextPath = path;

if (Array.isArray(nextPath)) {
nextPath = [this.key, ...nextPath];
} else {
nextPath = `${this.key}.${nextPath}`;
}
return nextPath;
}
}

export default HashPersist;
Loading