https://github.com/raiseandfall/hash.subscribe
Subscribe anything to the browser Hash location changes.
https://github.com/raiseandfall/hash.subscribe
hash npm npm-package url-hash
Last synced: 29 days ago
JSON representation
Subscribe anything to the browser Hash location changes.
- Host: GitHub
- URL: https://github.com/raiseandfall/hash.subscribe
- Owner: raiseandfall
- License: mit
- Created: 2015-08-19T18:40:00.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2017-07-10T12:45:06.000Z (almost 9 years ago)
- Last Synced: 2025-10-19T11:34:11.775Z (8 months ago)
- Topics: hash, npm, npm-package, url-hash
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/hash.subscribe
- Size: 37.1 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Hash.subscribe
[](https://travis-ci.org/raiseandfall/hash.subscribe)
[](https://github.com/Flet/semistandard) [](https://www.npmjs.com/package/hash.subscribe)
A browser hash listener system using query string style parameters ( `#foo=bar&baz=qux` ), based on the subscribe design pattern.
Features:
- Browser Hash initialization
- Subscribe to Hash specific parameters
- Set specific Hash parameter
- Mute / Unmute subscription
## [CHANGELOG](./CHANGELOG.md)
## INSTALL
```shell
$ npm install hash.subscribe
```
## USAGE
```javascript
var Hash = require('hash.subscribe');
// Init browser hash
Hash.init('foo=bar');
// Set parameters (object)
Hash.setHash({
foo: ['bar'],
baz: ['qux']
});
// Set hash (string)
Hash.setHash('foo=bar&baz=qux');
// Update one key value
Hash.updateHashKeyValue('foo', ['bar1', 'bar2']);
// Delete one param
Hash.deleteParam('baz');
// Subscribe to parameter(s)
Hash.subscribe(['foo', 'baz'], function(params) {
if (params.foo.changed) {
console.log('Param Foo has changed : ' + params.foo.values);
}
});
// More examples in the test file...
```
## API
### `init(hash)`
Parameter:
- `hash` - String | Object - default hash to initialize the page with
### `getHash(keepHash)`
Get current hash
Parameter:
- `keepHash` - Boolean - whether or not to keep the hash character in the return string
Returns: String
### `getParams`
Get current hash parameters
Returns: Object
### `getParam(key)`
Get values of one hash parameter
Parameter:
- `key` - string - parameter name
Returns: Array
### `setHash(hash)`
Updates current hash
Parameter:
- `hash` - String | Object - new hash
### `updateHashKeyValue(key, value)`
Updates one hash key
Parameters:
- `key` - String - hash key to update
- `value` - Array - new value(s) for key
### `deleteParam(params)`
Deletes hash param(s)
Parameter:
- `params` - String | Array - param(s) name(s) to delete
### `subscribe([hashParameters], callback)`
Subscribe to specific parameters
Parameters:
- `hashParameters` - Array - Array of parameters names you want to subscribe to
- `callback` - Function - Callback function called when a parameter has changed
### `mute()`
Mutes subscription to Hash
### `unmute()`
Resume subscription to Hash
### `destroy()`
Destroys current hash value
## CONTRIBUTE
### Development task
```shell
$ git clone git@github.com:raiseandfall/Hash.subscribe.git && cd Hash.subscribe
$ npm run dev
```
### Run tests independently
```shell
$ npm run test
```
## LICENSE
MIT