Library Reference
JS Library
Dictionary Engine
14min
the dictionary engine is the one used by multiple other engines to hold and retrieve data in a key/value format translation engine currency engine cache engine available interfaces getglobal – gets the global stored elements mergetoglobal – retrieves the elements from local and merges them into the global storage elements overrideglobal – retrieves the elements from local and overrides them into the global storage elements mergefromglobal – retrieves the elements from storage and merges them into the local storage elements setelements – calls addelement for a list of elements to be added getelements – gets all elements resetelements – resets all elements(deletes local elements) getelement – gets one value for a given key of a element addelement – adds to local dictionary a element in the key value format setstorage – defines a storage for engine for the storage of the global elements of the dictionary the dictionary is a key value implementation that can retrieve a value for a specific key of string or number type the key has to be unique within the scope of the generated dictionary if same key is added it will override the local storage element that has that key the scope is defined by the name given on generation the scope is only taken in consideration when interacting with the global storage of the dictionary , local storage per element can differ from one instance to another of a dictionary that is part of same scope any local change can be pushed to the global storage via the interfaces provided any other instance with the same scope will need to merge those changes into there local storage as needed example using dictionary by setting an element to a dictionary and then retrieving it from another instance of the same dictionary var testdictionary = klevu dictionary("test"); testdictionary getelements(); // {} testdictionary addelement("test","testing"); testdictionary mergetoglobal(); testdictionary getglobal(); // {test "testing"} testdictionary getelements(); // {test "testing"} var testnewdictionary = klevu dictionary("test") testnewdictionary getelements(); // {} testnewdictionary getglobal(); // {test "testing"} testnewdictionary mergefromglobal(); testnewdictionary getelements(); // {test "testing") dictionary setstorage() sets the dictionary engine storage types for its global scope storage – keyword for the storage used fallback – boolean – optional – use cookie storage as fallback if using required storage is not supported current supported storage engines are object default local – uses browser local storage session – uses browser session storage cookies – uses browser cookies , persistent across session cookiessession – uses browser cookies , only for current session session setting a storage will also retrieve all data from the storage on the global scope as a example if the global scope is empty but the dictionary data has been saved in the local storage previously var testdictionary = klevu dictionary("test"); testdictionary getglobal(); // {} testdictionary setstorage("local", true); testdictionary getglobal(); // {test "test"} dictionary getglobal() used to retrieve the values directly from the global scope the values retrieved will still belong to the global scope and not will be part of the local scope for example consider that global is {test “test”} var testdictionary = klevu dictionary("test"); testdictionary getglobal(); // {test "test"} testdictionary getelements(); // {} dictionary mergefromglobal() used to retrieve the values from the global scope and merge it over the local scope if a key is already present in the local scope with a different value it will be overridden by the value in the global scope // consider that global is {test "test"} var testdictionary = klevu dictionary("test"); testdictionary getelements(); // {} testdictionary addelement("test","testing"); testdictionary getelements(); // {test "testing"} testdictionary mergefromglobal(); testdictionary getelements(); // {test "test"} currency engine the currency engine is an implementation of the dictionary engine refer to the dictionary docs for an in depth view the map of a currency element, with defaults atend false – controls if the symbol is after the value code “\<string>” – same as the global name decimal “ ” – symbol to be used in the decimal place format “ %s%s ” – the format of the currency, can only contain 2 elements grouping 3 – thousand decimal grouping precision 2 – rounding on x decimal string “\<symbol>” – the symbol to be used for the currency thousand “ , ” – symbol to be used in the thousand place specific functions for the engine addcurrency – used to add a single currency object – addcurrency function ( code , map ) setcurrencies – used to set a list of currencies processcurrency – retrieves the string of the formatted value in the selected currency code – processcurrency function ( code , value ) all of the dictionary engine interfaces are available on the currency engine translator engine the translator engine is an implementation of the dictionary engine refer to the dictionary docs for an in depth view the translator engine is used to group a specific language and the currency engine used in that language specific functions for the engine translate – used to reverse check a string from the dictionary, the original string is returned if no string matches dictionary accepts parameters to be used as format strings – example translate(“i have found %s products”,5) would return “i have found 5 products” getcurrencyobject – retrieves the currency object for the translation object all of the dictionary engine interfaces are available on the translator engine example klevu search landing getscope() translator; klevu search landing getscope() template gettranslator(); cache engine stores a key value map ,the engine keys do not have to be of string type but can be any object , similar to map any value (both objects and primitive values) may be used as either a key or a value the engine is used to store individual query request/response the scope of the cache is always global and will be shared by all implementations of the cache engine methods getcache – retrieves a value for a given key object hascache – checks if a given key object has a value setcache – sets a key/value map to the cache engine getallcache – get all cache objects example var cache = klevu cache(); cache setcache({request "value"},{responce "value"}); cache hascache({request "value"}); // true cache getcache({request "value"}); // {responce "value"}