Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alexandremasy/spices-anise
URL Parser
https://github.com/alexandremasy/spices-anise
Last synced: about 1 hour ago
JSON representation
URL Parser
- Host: GitHub
- URL: https://github.com/alexandremasy/spices-anise
- Owner: alexandremasy
- Created: 2020-09-16T10:15:00.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-09-16T11:14:02.000Z (over 4 years ago)
- Last Synced: 2024-11-06T19:55:51.331Z (about 2 months ago)
- Language: JavaScript
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Spices - Anise
> Javascript utility library for parsing the document.location## Usage
```JS
import {Anise, Field} from '@spices/anise'let args = Anise.detect({
new Field({
name: 'lang',
fallback: 'en',
path: /.?\/(de|en|es)\/?.?/,
persistent: true,
search: /lang=(de|en|es)/,
values: ['de', 'en', 'es'],
})
});
```Will yield
- `/` -> `{lang="en"}` - Because `en` is the `fallback` value .
- `/fr` -> `{lang="en"}` - Because `fr` is not in the value list, therefor it takes only the valid values.
- `/en?lang=de` -> `{lang="de"}` - Because the search values will override the path values.## Field
- `fallback` - The fallback if the field is not found. The default value.
- `name` - The value used to identify the field
- `path` | - The path regexp to test against. Or the function to call to check whether or not the arguments is part of the pathname
- `persistent` - Whether or not use the localstorage are a source and provider
- `search` | The search regexp to test against. Or the function to call to check whether or not the arguments is part of the search query string
- `validator` A function use to determine Whether or not the value detected is valid or not
- `values` A list of allowed values for the field. Will be used to check against for validity.## How does it work?
When triggering the method `Anise.detect`, @spices/anise will check for the fields in the following sequence:
1. Use the `fallback` to setup the default values
2. Look for the `persistent` values in the localStorage
3. Look at the `document.location.pathname` for field matching the `path`
4. Look at the `document.location.search` for field mathing the `search`
5. Persists the information when needed