Javascript utility library for parsing the document.location
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"}
- Becauseen
is thefallback
value ./fr
->{lang="en"}
- Becausefr
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.
fallback
- The fallback if the field is not found. The default value.name
- The value used to identify the fieldpath
| - The path regexp to test against. Or the function to call to check whether or not the arguments is part of the pathnamepersistent
- Whether or not use the localstorage are a source and providersearch
| The search regexp to test against. Or the function to call to check whether or not the arguments is part of the search query stringvalidator
A function use to determine Whether or not the value detected is valid or notvalues
A list of allowed values for the field. Will be used to check against for validity.
When triggering the method Anise.detect
, @spices/anise will check for the fields in the following sequence:
- Use the
fallback
to setup the default values - Look for the
persistent
values in the localStorage - Look at the
document.location.pathname
for field matching thepath
- Look at the
document.location.search
for field mathing thesearch
- Persists the information when needed