@ideal-postcodes/jsutil
Advanced tools
Comparing version 4.0.0 to 4.0.1
@@ -0,1 +1,8 @@ | ||
## [4.0.1](https://github.com/ideal-postcodes/jsutil/compare/4.0.0...4.0.1) (2021-01-18) | ||
### Bug Fixes | ||
* **includes:** Add fn for common polyfill ([9d351b6](https://github.com/ideal-postcodes/jsutil/commit/9d351b6fac1be5528fe1845c939de15fc4f4fbe1)) | ||
# [4.0.0](https://github.com/ideal-postcodes/jsutil/compare/3.1.0...4.0.0) (2021-01-15) | ||
@@ -2,0 +9,0 @@ |
@@ -8,3 +8,3 @@ "use strict"; | ||
const country_1 = require("./country"); | ||
const util_1 = require("./util"); | ||
const string_1 = require("./string"); | ||
/** | ||
@@ -15,3 +15,3 @@ * Updates address line fields given result and number of available target lines | ||
*/ | ||
exports.updateAddressLines = (targets, address) => { | ||
const updateAddressLines = (targets, address) => { | ||
const [line_1, line_2, line_3] = exports.toAddressLines(exports.numberOfLines(targets), address); | ||
@@ -22,2 +22,3 @@ input_1.update(targets.line_1, line_1, true); | ||
}; | ||
exports.updateAddressLines = updateAddressLines; | ||
/** | ||
@@ -28,3 +29,3 @@ * Populate an address based on selector targets | ||
*/ | ||
exports.addressRetrieval = ({ targets, config }) => (address) => { | ||
const addressRetrieval = ({ targets, config }) => (address) => { | ||
exports.updateAddressLines(targets, address); | ||
@@ -39,6 +40,7 @@ input_1.update(targets.post_town, address.post_town); | ||
}; | ||
exports.addressRetrieval = addressRetrieval; | ||
/** | ||
* Returns number of lines | ||
*/ | ||
exports.numberOfLines = (targets) => { | ||
const numberOfLines = (targets) => { | ||
const { line_2, line_3 } = targets; | ||
@@ -51,8 +53,9 @@ if (!line_2) | ||
}; | ||
exports.numberOfLines = numberOfLines; | ||
/** | ||
* Removes empty fields and joins | ||
*/ | ||
exports.join = (list) => list | ||
const join = (list) => list | ||
.filter((e) => { | ||
if (util_1.isString(e)) | ||
if (string_1.isString(e)) | ||
return !!e.trim(); | ||
@@ -62,3 +65,4 @@ return !!e; | ||
.join(", "); | ||
exports.toAddressLines = (n, address) => { | ||
exports.join = join; | ||
const toAddressLines = (n, address) => { | ||
const { line_1, line_2, line_3 } = address; | ||
@@ -71,6 +75,7 @@ if (n === 3) | ||
}; | ||
exports.toAddressLines = toAddressLines; | ||
/** | ||
* Get address property value | ||
*/ | ||
exports.extract = (a, attr) => { | ||
const extract = (a, attr) => { | ||
const result = a[attr]; | ||
@@ -83,6 +88,8 @@ if (typeof result === "number") | ||
}; | ||
exports.extract = extract; | ||
/** | ||
* Check if address values exists | ||
*/ | ||
exports.notInAddress = (o, attr) => o[attr] === undefined; | ||
const notInAddress = (o, attr) => o[attr] === undefined; | ||
exports.notInAddress = notInAddress; | ||
/** | ||
@@ -102,3 +109,3 @@ * Update address lines | ||
*/ | ||
exports.populateAddress = (options) => { | ||
const populateAddress = (options) => { | ||
const { outputFields, config } = options; | ||
@@ -133,2 +140,3 @@ const address = { ...options.address }; | ||
}; | ||
exports.populateAddress = populateAddress; | ||
/** | ||
@@ -140,3 +148,3 @@ * Mutates an address object to remove an organisation name from address | ||
*/ | ||
exports.removeOrganisation = (address) => { | ||
const removeOrganisation = (address) => { | ||
if (address.organisation_name.length === 0) | ||
@@ -154,1 +162,2 @@ return address; | ||
}; | ||
exports.removeOrganisation = removeOrganisation; |
@@ -8,3 +8,3 @@ "use strict"; | ||
*/ | ||
exports.downloadAutocompleteStyle = (d) => { | ||
const downloadAutocompleteStyle = (d) => { | ||
if (autocompleteLink !== undefined) | ||
@@ -17,6 +17,7 @@ return autocompleteLink; | ||
}; | ||
exports.downloadAutocompleteStyle = downloadAutocompleteStyle; | ||
/** | ||
* Returns true if autocomplete library present | ||
*/ | ||
exports.autocompletePresent = (w) => { | ||
const autocompletePresent = (w) => { | ||
if (w.IdealPostcodes === undefined) | ||
@@ -30,6 +31,7 @@ return false; | ||
}; | ||
exports.autocompletePresent = autocompletePresent; | ||
/** | ||
* Retrieves remote autocomplete script and CSS | ||
*/ | ||
exports.loadAutocomplete = (config) => { | ||
const loadAutocomplete = (config) => { | ||
// Exit if not available | ||
@@ -44,10 +46,12 @@ if (config.autocomplete !== true) | ||
}; | ||
exports.loadAutocomplete = loadAutocomplete; | ||
/** | ||
* Returns true if jQuery present | ||
*/ | ||
exports.jQueryPresent = (w) => w.jQuery !== undefined; | ||
const jQueryPresent = (w) => w.jQuery !== undefined; | ||
exports.jQueryPresent = jQueryPresent; | ||
/** | ||
* Return true if postcode lookup plugin present | ||
*/ | ||
exports.postcodeLookupPresent = (w) => { | ||
const postcodeLookupPresent = (w) => { | ||
if (exports.jQueryPresent(w) === false) | ||
@@ -57,2 +61,3 @@ return false; | ||
}; | ||
exports.postcodeLookupPresent = postcodeLookupPresent; | ||
/** | ||
@@ -62,3 +67,3 @@ * Retrieves remote postcode lookup script. Can be repeatedly called in a loop | ||
*/ | ||
exports.loadPostcodeLookup = (config) => { | ||
const loadPostcodeLookup = (config) => { | ||
if (config.postcodeLookup !== true) | ||
@@ -73,4 +78,5 @@ return "complete"; | ||
}; | ||
exports.loadPostcodeLookup = loadPostcodeLookup; | ||
const cache = {}; | ||
exports.clearCache = () => { | ||
const clearCache = () => { | ||
for (const url of Object.keys(cache)) { | ||
@@ -81,6 +87,7 @@ delete cache[url]; | ||
}; | ||
exports.clearCache = clearCache; | ||
/** | ||
* Script downloader factory. Caches script reference, only downloads once | ||
*/ | ||
exports.downloadScript = (url, integrity) => (d) => { | ||
const downloadScript = (url, integrity) => (d) => { | ||
if (cache[url]) | ||
@@ -94,2 +101,3 @@ return cache[url]; | ||
}; | ||
exports.downloadScript = downloadScript; | ||
/** | ||
@@ -110,3 +118,3 @@ * Download jQuery 3.5.1 | ||
*/ | ||
exports.loadStyle = (href, document) => { | ||
const loadStyle = (href, document) => { | ||
const link = document.createElement("link"); | ||
@@ -118,6 +126,7 @@ link.type = "text/css"; | ||
}; | ||
exports.loadStyle = loadStyle; | ||
/** | ||
* Inject script tag | ||
*/ | ||
exports.loadScript = (src, integrity, document) => { | ||
const loadScript = (src, integrity, document) => { | ||
const script = document.createElement("script"); | ||
@@ -130,2 +139,3 @@ script.type = "text/javascript"; | ||
}; | ||
exports.loadScript = loadScript; | ||
/** | ||
@@ -136,3 +146,3 @@ * Returns true if all necessary assets are available | ||
*/ | ||
exports.readyAssets = ({ config, window }) => { | ||
const readyAssets = ({ config, window }) => { | ||
let ready = true; | ||
@@ -152,6 +162,7 @@ // Retrieve assets | ||
}; | ||
exports.readyAssets = readyAssets; | ||
/** | ||
* Injects styke element to page | ||
*/ | ||
exports.injectStyle = (css, document) => { | ||
const injectStyle = (css, document) => { | ||
const style = document.createElement("style"); | ||
@@ -163,1 +174,2 @@ style.type = "text/css"; | ||
}; | ||
exports.injectStyle = injectStyle; |
@@ -8,3 +8,3 @@ "use strict"; | ||
*/ | ||
exports.toCiIso = (address) => { | ||
const toCiIso = (address) => { | ||
if (/^GY/.test(address.postcode)) | ||
@@ -16,2 +16,3 @@ return "GG"; | ||
}; | ||
exports.toCiIso = toCiIso; | ||
const UK = "United Kingdom"; | ||
@@ -29,3 +30,3 @@ const IOM = "Isle of Man"; | ||
*/ | ||
exports.toIso = (address) => { | ||
const toIso = (address) => { | ||
const country = address.country; | ||
@@ -46,2 +47,3 @@ if (country === EN) | ||
}; | ||
exports.toIso = toIso; | ||
/** | ||
@@ -52,3 +54,3 @@ * Maps UkCountry to Country | ||
*/ | ||
exports.toCountry = (address) => { | ||
const toCountry = (address) => { | ||
const country = address.country; | ||
@@ -74,6 +76,7 @@ if (country === EN) | ||
}; | ||
exports.toCountry = toCountry; | ||
/** | ||
* Updates country in BC country field given address selected | ||
*/ | ||
exports.updateCountry = (select, address) => { | ||
const updateCountry = (select, address) => { | ||
if (!select) | ||
@@ -94,1 +97,2 @@ return; | ||
}; | ||
exports.updateCountry = updateCountry; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getDocument = exports.getScope = exports.toElem = exports.insertBefore = exports.getAnchors = exports.toHtmlElem = exports.getParent = exports.markLoaded = exports.loaded = exports.toArray = void 0; | ||
const util_1 = require("./util"); | ||
const string_1 = require("./string"); | ||
/** | ||
* Convert a NodeList to array | ||
*/ | ||
exports.toArray = (nodeList) => Array.prototype.slice.call(nodeList); | ||
const toArray = (nodeList) => Array.prototype.slice.call(nodeList); | ||
exports.toArray = toArray; | ||
/** | ||
* Returns true if initialised | ||
*/ | ||
exports.loaded = (elem) => elem.getAttribute("idpc") === "true"; | ||
const loaded = (elem) => elem.getAttribute("idpc") === "true"; | ||
exports.loaded = loaded; | ||
/** | ||
* Marks HTML as loaded. i.e. Address validation plugin has been bound to this element | ||
*/ | ||
exports.markLoaded = (elem) => elem.setAttribute("idpc", "true"); | ||
const markLoaded = (elem) => elem.setAttribute("idpc", "true"); | ||
exports.markLoaded = markLoaded; | ||
const isTrue = () => true; | ||
@@ -25,3 +28,3 @@ /** | ||
*/ | ||
exports.getParent = (node, entity, test = isTrue) => { | ||
const getParent = (node, entity, test = isTrue) => { | ||
let parent = node; | ||
@@ -38,6 +41,8 @@ const tagName = entity.toUpperCase(); | ||
}; | ||
exports.getParent = getParent; | ||
/** | ||
* Queries `parent` for a specific `selector`. Returns null if no selector | ||
*/ | ||
exports.toHtmlElem = (parent, selector) => (selector ? parent.querySelector(selector) : null); | ||
const toHtmlElem = (parent, selector) => (selector ? parent.querySelector(selector) : null); | ||
exports.toHtmlElem = toHtmlElem; | ||
/** | ||
@@ -48,3 +53,3 @@ * Retrieves an anchor defined by a query selector | ||
*/ | ||
exports.getAnchors = (selector, d) => { | ||
const getAnchors = (selector, d) => { | ||
const matches = (d || window.document).querySelectorAll(selector); | ||
@@ -57,6 +62,7 @@ const anchors = exports.toArray(matches).filter((e) => !exports.loaded(e)); | ||
}; | ||
exports.getAnchors = getAnchors; | ||
/** | ||
* Inserts element before `target` element | ||
*/ | ||
exports.insertBefore = ({ elem, target }) => { | ||
const insertBefore = ({ elem, target }) => { | ||
const parent = target.parentNode; | ||
@@ -68,2 +74,3 @@ if (parent === null) | ||
}; | ||
exports.insertBefore = insertBefore; | ||
/** | ||
@@ -74,7 +81,8 @@ * Scoped retrieval of a HTML element given a querystring or the element itself | ||
*/ | ||
exports.toElem = (elem, context) => { | ||
if (util_1.isString(elem)) | ||
const toElem = (elem, context) => { | ||
if (string_1.isString(elem)) | ||
return context.querySelector(elem); | ||
return elem; | ||
}; | ||
exports.toElem = toElem; | ||
/** | ||
@@ -90,4 +98,4 @@ * @hidden | ||
*/ | ||
exports.getScope = (scope) => { | ||
if (util_1.isString(scope)) | ||
const getScope = (scope) => { | ||
if (string_1.isString(scope)) | ||
return d.querySelector(scope); | ||
@@ -98,2 +106,3 @@ if (scope === null) | ||
}; | ||
exports.getScope = getScope; | ||
/** | ||
@@ -104,3 +113,3 @@ * Retrieves document instance of HTML Element or Document | ||
*/ | ||
exports.getDocument = (scope) => { | ||
const getDocument = (scope) => { | ||
if (scope instanceof Document) | ||
@@ -112,1 +121,2 @@ return scope; | ||
}; | ||
exports.getDocument = getDocument; |
@@ -9,3 +9,3 @@ "use strict"; | ||
*/ | ||
exports.newEvent = ({ event, bubbles = true, cancelable = true, }) => { | ||
const newEvent = ({ event, bubbles = true, cancelable = true, }) => { | ||
// if Event available | ||
@@ -19,5 +19,7 @@ if (typeof window.Event === "function") | ||
}; | ||
exports.newEvent = newEvent; | ||
/** | ||
* Dispatch custom event on element | ||
*/ | ||
exports.trigger = (e, event) => e.dispatchEvent(exports.newEvent({ event })); | ||
const trigger = (e, event) => e.dispatchEvent(exports.newEvent({ event })); | ||
exports.trigger = trigger; |
@@ -18,6 +18,8 @@ "use strict"; | ||
} | ||
exports.global = () => g; | ||
const global = () => g; | ||
exports.global = global; | ||
/** | ||
* Resets global store | ||
*/ | ||
exports.reset = () => Object.getOwnPropertyNames(g).forEach((p) => delete g[p]); | ||
const reset = () => Object.getOwnPropertyNames(g).forEach((p) => delete g[p]); | ||
exports.reset = reset; |
@@ -8,3 +8,3 @@ "use strict"; | ||
*/ | ||
exports.idGen = (prefix = "idpc_") => () => { | ||
const idGen = (prefix = "idpc_") => () => { | ||
const g = global_1.global(); | ||
@@ -18,1 +18,2 @@ if (!g.idGen) | ||
}; | ||
exports.idGen = idGen; |
@@ -11,1 +11,2 @@ export * from "./util"; | ||
export * from "./address"; | ||
export * from "./string"; |
@@ -23,1 +23,2 @@ "use strict"; | ||
__exportStar(require("./address"), exports); | ||
__exportStar(require("./string"), exports); |
@@ -8,3 +8,3 @@ "use strict"; | ||
*/ | ||
exports.isSelect = (e) => { | ||
const isSelect = (e) => { | ||
if (e === null) | ||
@@ -14,6 +14,7 @@ return false; | ||
}; | ||
exports.isSelect = isSelect; | ||
/** | ||
* Returns true if Element is <input> | ||
*/ | ||
exports.isInput = (e) => { | ||
const isInput = (e) => { | ||
if (e === null) | ||
@@ -23,6 +24,7 @@ return false; | ||
}; | ||
exports.isInput = isInput; | ||
/** | ||
* Updates input value and dispatches change envet | ||
*/ | ||
exports.update = (input, value, skipTrigger = false) => { | ||
const update = (input, value, skipTrigger = false) => { | ||
if (!input) | ||
@@ -34,6 +36,7 @@ return; | ||
}; | ||
exports.update = update; | ||
/** | ||
* Returns true if HTMLElement has matching value | ||
*/ | ||
exports.hasValue = (select, value) => { | ||
const hasValue = (select, value) => { | ||
if (value === null) | ||
@@ -43,2 +46,3 @@ return false; | ||
}; | ||
exports.hasValue = hasValue; | ||
/** | ||
@@ -62,3 +66,3 @@ * Updates value property of HTMLSelectElement | ||
*/ | ||
exports.setValue = (e, value) => { | ||
const setValue = (e, value) => { | ||
e.value = value; | ||
@@ -68,2 +72,3 @@ if (e.value !== value) | ||
}; | ||
exports.setValue = setValue; | ||
/** | ||
@@ -73,3 +78,3 @@ * Sets the value of an Element using the setter for value found on the | ||
*/ | ||
exports.forceSetValue = (e, value) => { | ||
const forceSetValue = (e, value) => { | ||
const descriptor = Object.getOwnPropertyDescriptor(e.constructor.prototype, "value"); | ||
@@ -83,2 +88,3 @@ if (descriptor === undefined) | ||
}; | ||
exports.forceSetValue = forceSetValue; | ||
/** | ||
@@ -102,3 +108,3 @@ * Updates value property of HTMLInputElement | ||
*/ | ||
exports.change = (options) => { | ||
const change = (options) => { | ||
if (options.value === null) | ||
@@ -109,1 +115,2 @@ return; | ||
}; | ||
exports.change = change; |
@@ -19,3 +19,2 @@ import { Bind, Start, ParentTest, Stop, PageTest, Config, Selectors, Targets, Binding } from "./types"; | ||
export declare const config: () => Config | undefined; | ||
export declare const isString: (input: unknown) => input is string; | ||
interface GenerateTimerOptions { | ||
@@ -22,0 +21,0 @@ pageTest: PageTest; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toId = exports.setupBind = exports.setup = exports.generateTimer = exports.isString = exports.config = exports.defaults = exports.relevantPage = exports.getTargets = void 0; | ||
exports.toId = exports.setupBind = exports.setup = exports.generateTimer = exports.config = exports.defaults = exports.relevantPage = exports.getTargets = void 0; | ||
const dom_1 = require("./dom"); | ||
@@ -12,3 +12,3 @@ /** | ||
*/ | ||
exports.getTargets = (parent, selectors) => { | ||
const getTargets = (parent, selectors) => { | ||
const line_1 = dom_1.toHtmlElem(parent, selectors.line_1); | ||
@@ -39,6 +39,8 @@ if (line_1 === null) | ||
}; | ||
exports.getTargets = getTargets; | ||
/** | ||
* Given a list of bindings, return true if any are relevant to current pae | ||
*/ | ||
exports.relevantPage = (bindings) => bindings.some((b) => b.pageTest()); | ||
const relevantPage = (bindings) => bindings.some((b) => b.pageTest()); | ||
exports.relevantPage = relevantPage; | ||
/** | ||
@@ -59,3 +61,3 @@ * Setup defaults | ||
}; | ||
exports.config = () => { | ||
const config = () => { | ||
const c = window.idpcConfig; | ||
@@ -66,4 +68,4 @@ if (c === undefined) | ||
}; | ||
exports.isString = (input) => typeof input === "string"; | ||
exports.generateTimer = ({ pageTest, bind }) => { | ||
exports.config = config; | ||
const generateTimer = ({ pageTest, bind }) => { | ||
let timer = null; | ||
@@ -94,2 +96,3 @@ const start = (config) => { | ||
}; | ||
exports.generateTimer = generateTimer; | ||
const NOOP = () => { }; | ||
@@ -99,3 +102,3 @@ /** | ||
*/ | ||
exports.setup = ({ bindings, callback = NOOP }) => { | ||
const setup = ({ bindings, callback = NOOP }) => { | ||
const c = exports.config(); | ||
@@ -117,5 +120,6 @@ if (c === undefined) | ||
}; | ||
exports.setup = setup; | ||
const DEFAULT_SCOPE = "form"; | ||
const DEFAULT_ANCHOR = "line_1"; | ||
exports.setupBind = ({ selectors, anchorSelector, parentScope, doc, parentTest, }) => { | ||
const setupBind = ({ selectors, anchorSelector, parentScope, doc, parentTest, }) => { | ||
const anchors = dom_1.getAnchors(anchorSelector || selectors[DEFAULT_ANCHOR], doc); | ||
@@ -133,2 +137,4 @@ return anchors.reduce((prev, anchor) => { | ||
}; | ||
exports.toId = (elem) => `#${elem.id}`; | ||
exports.setupBind = setupBind; | ||
const toId = (elem) => `#${elem.id}`; | ||
exports.toId = toId; |
@@ -5,3 +5,3 @@ import { capitalisePostTown } from "capitalise-post-town"; | ||
import { updateCountry } from "./country"; | ||
import { isString } from "./util"; | ||
import { isString } from "./string"; | ||
/** | ||
@@ -8,0 +8,0 @@ * Updates address line fields given result and number of available target lines |
@@ -1,2 +0,2 @@ | ||
import { isString } from "./util"; | ||
import { isString } from "./string"; | ||
/** | ||
@@ -3,0 +3,0 @@ * Convert a NodeList to array |
@@ -11,1 +11,2 @@ export * from "./util"; | ||
export * from "./address"; | ||
export * from "./string"; |
@@ -11,1 +11,2 @@ export * from "./util"; | ||
export * from "./address"; | ||
export * from "./string"; |
@@ -19,3 +19,2 @@ import { Bind, Start, ParentTest, Stop, PageTest, Config, Selectors, Targets, Binding } from "./types"; | ||
export declare const config: () => Config | undefined; | ||
export declare const isString: (input: unknown) => input is string; | ||
interface GenerateTimerOptions { | ||
@@ -22,0 +21,0 @@ pageTest: PageTest; |
@@ -60,3 +60,2 @@ import { toHtmlElem, getAnchors, getParent } from "./dom"; | ||
}; | ||
export const isString = (input) => typeof input === "string"; | ||
export const generateTimer = ({ pageTest, bind }) => { | ||
@@ -63,0 +62,0 @@ let timer = null; |
{ | ||
"name": "@ideal-postcodes/jsutil", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"description": "Browser Address Autocomplete for api.ideal-postcodes.co.uk", | ||
@@ -35,2 +35,6 @@ "author": { | ||
}, | ||
"browserslist": [ | ||
"ie 11", | ||
"last 2 versions" | ||
], | ||
"files": [ | ||
@@ -111,3 +115,3 @@ "dist", | ||
"@types/puppeteer": "~5.4.0", | ||
"@typescript-eslint/eslint-plugin": "^4.0.0", | ||
"@typescript-eslint/eslint-plugin": "~4.0.0", | ||
"agadoo": "~2.0.0", | ||
@@ -119,3 +123,4 @@ "babel": "~6.23.0", | ||
"dotenv": "~8.2.0", | ||
"eslint": "~7.17.0", | ||
"eslint": "~7.18.0", | ||
"eslint-plugin-compat": "~3.9.0", | ||
"karma": "~5.2.2", | ||
@@ -138,4 +143,4 @@ "karma-babel-preprocessor": "~8.0.1", | ||
"typedoc": "~0.20.14", | ||
"typescript": "~4.0.3" | ||
"typescript": "~4.1.3" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
91441
51
2708
41