@ideal-postcodes/jsutil
Advanced tools
Comparing version 6.2.0 to 6.3.0
@@ -0,1 +1,10 @@ | ||
# [6.3.0](https://github.com/ideal-postcodes/jsutil/compare/6.2.0...6.3.0) (2023-08-30) | ||
### Features | ||
* **lines:** Limit number character in one address line ([81c2a5b](https://github.com/ideal-postcodes/jsutil/commit/81c2a5ba3c04315db9ec8b56052f73f1eb2d1360)) | ||
* **Max Length:** Add max length ([742b9a8](https://github.com/ideal-postcodes/jsutil/commit/742b9a861ada10234945ddf0cff6788da985f606)) | ||
* **workflows:** Drop lint workflow ([e12f374](https://github.com/ideal-postcodes/jsutil/commit/e12f374bb94f7da20676d0921638e99dc995aece)) | ||
# [6.2.0](https://github.com/ideal-postcodes/jsutil/compare/6.1.0...6.2.0) (2023-07-19) | ||
@@ -2,0 +11,0 @@ |
import { AnyAddress, GbrAddress } from "./types"; | ||
import { Targets, OutputFields, LineCount, Config, NamedFields } from "./types"; | ||
export declare const updateAddressLines: (targets: Targets, address: AnyAddress) => void; | ||
interface Options { | ||
targets: Targets; | ||
config: Config; | ||
import { Targets, OutputFields, LineCount, NamedFields } from "./types"; | ||
export declare const numberOfLines: (targets: Targets) => LineCount; | ||
export declare const join: (list: unknown[]) => string; | ||
export declare const charArrayLength: (arr: string[]) => number; | ||
interface MaxLengthOptions extends MaxLineConfig { | ||
lineCount: number; | ||
} | ||
interface AddressRetrieval { | ||
(options: Options): (a: GbrAddress) => void; | ||
export declare const toMaxLengthLines: (l: string[], options: MaxLengthOptions) => [string, string, string]; | ||
export declare type MaxLengths = [number?, number?, number?]; | ||
interface MaxLengthOptions { | ||
} | ||
export declare const addressRetrieval: AddressRetrieval; | ||
export declare const numberOfLines: (targets: Targets) => LineCount; | ||
export declare const join: (list: unknown[]) => string; | ||
export declare const toAddressLines: (n: LineCount, address: AnyAddress) => [string, string, string]; | ||
export declare const toAddressLines: (lineCount: LineCount, address: AnyAddress, options: MaxLineConfig) => [string, string, string]; | ||
declare type KeysOfUnion<T> = T extends T ? keyof T : never; | ||
export declare const extract: (a: AnyAddress, attr: KeysOfUnion<AnyAddress>) => string; | ||
export declare const getFields: (o: PopulateAddressOptions) => OutputFields; | ||
export interface PopulateConfig { | ||
export interface MaxLineConfig { | ||
maxLineOne?: number; | ||
maxLineTwo?: number; | ||
maxLineThree?: number; | ||
} | ||
export interface PopulateConfig extends MaxLineConfig { | ||
scope: HTMLElement | Document; | ||
@@ -20,0 +24,0 @@ titleizePostTown?: boolean; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.removeOrganisation = exports.populateAddress = exports.searchLabels = exports.searchNames = exports.getFields = exports.extract = exports.toAddressLines = exports.join = exports.numberOfLines = exports.addressRetrieval = exports.updateAddressLines = void 0; | ||
exports.removeOrganisation = exports.populateAddress = exports.searchLabels = exports.searchNames = exports.getFields = exports.extract = exports.toAddressLines = exports.toMaxLengthLines = exports.charArrayLength = exports.join = exports.numberOfLines = void 0; | ||
const capitalise_post_town_1 = require("capitalise-post-town"); | ||
@@ -11,20 +11,2 @@ const input_1 = require("./input"); | ||
const util_1 = require("./util"); | ||
const updateAddressLines = (targets, address) => { | ||
const [line_1, line_2, line_3] = (0, exports.toAddressLines)((0, exports.numberOfLines)(targets), address); | ||
(0, input_1.update)(targets.line_1, line_1, true); | ||
(0, input_1.update)(targets.line_2, line_2); | ||
(0, input_1.update)(targets.line_3, line_3); | ||
}; | ||
exports.updateAddressLines = updateAddressLines; | ||
const addressRetrieval = ({ targets, config }) => (address) => { | ||
(0, exports.updateAddressLines)(targets, address); | ||
(0, input_1.update)(targets.post_town, address.post_town); | ||
(0, input_1.update)(targets.postcode, address.postcode); | ||
(0, country_1.updateCountry)(targets.country, address); | ||
if (config.populateOrganisation) | ||
(0, input_1.update)(targets.organisation, address.organisation_name); | ||
if (config.populateCounty) | ||
(0, input_1.update)(targets.county, address.county); | ||
}; | ||
exports.addressRetrieval = addressRetrieval; | ||
const numberOfLines = (targets) => { | ||
@@ -47,8 +29,78 @@ const { line_2, line_3 } = targets; | ||
exports.join = join; | ||
const toAddressLines = (n, address) => { | ||
const charArrayLength = (arr) => arr.join(" ").trim().length; | ||
exports.charArrayLength = charArrayLength; | ||
const truncate = (line, maxLength) => { | ||
if (line.length <= maxLength) | ||
return [line, ""]; | ||
const words = line.split(" "); | ||
let truncated = ""; | ||
let remaining = ""; | ||
for (let i = 0; i < words.length; i++) { | ||
const word = words[i]; | ||
if (truncated.length + word.length > maxLength) { | ||
remaining = words.slice(i).join(" "); | ||
break; | ||
} | ||
truncated += `${word} `; | ||
} | ||
return [truncated.trim(), remaining.trim()]; | ||
}; | ||
const prependLine = (remaining, nextLine) => { | ||
if (nextLine.length === 0) | ||
return remaining; | ||
return `${remaining}, ${nextLine}`; | ||
}; | ||
const toMaxLengthLines = (l, options) => { | ||
const { lineCount, maxLineOne, maxLineTwo, maxLineThree } = options; | ||
const result = ["", "", ""]; | ||
const lines = [...l]; | ||
if (maxLineOne) { | ||
const [newLineOne, remaining] = truncate(lines[0], maxLineOne); | ||
result[0] = newLineOne; | ||
if (remaining) | ||
lines[1] = prependLine(remaining, lines[1]); | ||
if (lineCount === 1) | ||
return result; | ||
} | ||
else { | ||
result[0] = lines[0]; | ||
if (lineCount === 1) | ||
return [(0, exports.join)(lines), "", ""]; | ||
} | ||
if (maxLineTwo) { | ||
const [newLineTwo, remaining] = truncate(lines[1], maxLineTwo); | ||
result[1] = newLineTwo; | ||
if (remaining) | ||
lines[2] = prependLine(remaining, lines[2]); | ||
if (lineCount === 2) | ||
return result; | ||
} | ||
else { | ||
result[1] = lines[1]; | ||
if (lineCount === 2) | ||
return [result[0], (0, exports.join)(lines.slice(1)), ""]; | ||
} | ||
if (maxLineThree) { | ||
const [newLineThree, remaining] = truncate(lines[2], maxLineThree); | ||
result[2] = newLineThree; | ||
if (remaining) | ||
lines[3] = prependLine(remaining, lines[3]); | ||
} | ||
else { | ||
result[2] = lines[2]; | ||
} | ||
return result; | ||
}; | ||
exports.toMaxLengthLines = toMaxLengthLines; | ||
const toAddressLines = (lineCount, address, options) => { | ||
const { line_1, line_2 } = address; | ||
const line_3 = "line_3" in address ? address.line_3 : ""; | ||
if (n === 3) | ||
if (options.maxLineOne || options.maxLineTwo || options.maxLineThree) | ||
return (0, exports.toMaxLengthLines)([line_1, line_2, line_3], { | ||
lineCount, | ||
...options, | ||
}); | ||
if (lineCount === 3) | ||
return [line_1, line_2, line_3]; | ||
if (n === 2) | ||
if (lineCount === 2) | ||
return [line_1, (0, exports.join)([line_2, line_3]), ""]; | ||
@@ -73,4 +125,4 @@ return [(0, exports.join)([line_1, line_2, line_3]), "", ""]; | ||
exports.getFields = getFields; | ||
const updateLines = (fields, address, scope) => { | ||
const [line_1, line_2, line_3] = (0, exports.toAddressLines)((0, exports.numberOfLines)(fields), address); | ||
const updateLines = (fields, address, scope, config) => { | ||
const [line_1, line_2, line_3] = (0, exports.toAddressLines)((0, exports.numberOfLines)(fields), address, config); | ||
(0, input_1.update)((0, dom_1.toElem)(fields.line_1 || null, scope), line_1); | ||
@@ -145,3 +197,3 @@ (0, input_1.update)((0, dom_1.toElem)(fields.line_2 || null, scope), line_2); | ||
} | ||
updateLines(fields, address, scope); | ||
updateLines(fields, address, scope, config); | ||
delete address.line_1; | ||
@@ -148,0 +200,0 @@ delete address.line_2; |
import { AnyAddress, GbrAddress } from "./types"; | ||
import { Targets, OutputFields, LineCount, Config, NamedFields } from "./types"; | ||
export declare const updateAddressLines: (targets: Targets, address: AnyAddress) => void; | ||
interface Options { | ||
targets: Targets; | ||
config: Config; | ||
import { Targets, OutputFields, LineCount, NamedFields } from "./types"; | ||
export declare const numberOfLines: (targets: Targets) => LineCount; | ||
export declare const join: (list: unknown[]) => string; | ||
export declare const charArrayLength: (arr: string[]) => number; | ||
interface MaxLengthOptions extends MaxLineConfig { | ||
lineCount: number; | ||
} | ||
interface AddressRetrieval { | ||
(options: Options): (a: GbrAddress) => void; | ||
export declare const toMaxLengthLines: (l: string[], options: MaxLengthOptions) => [string, string, string]; | ||
export declare type MaxLengths = [number?, number?, number?]; | ||
interface MaxLengthOptions { | ||
} | ||
export declare const addressRetrieval: AddressRetrieval; | ||
export declare const numberOfLines: (targets: Targets) => LineCount; | ||
export declare const join: (list: unknown[]) => string; | ||
export declare const toAddressLines: (n: LineCount, address: AnyAddress) => [string, string, string]; | ||
export declare const toAddressLines: (lineCount: LineCount, address: AnyAddress, options: MaxLineConfig) => [string, string, string]; | ||
declare type KeysOfUnion<T> = T extends T ? keyof T : never; | ||
export declare const extract: (a: AnyAddress, attr: KeysOfUnion<AnyAddress>) => string; | ||
export declare const getFields: (o: PopulateAddressOptions) => OutputFields; | ||
export interface PopulateConfig { | ||
export interface MaxLineConfig { | ||
maxLineOne?: number; | ||
maxLineTwo?: number; | ||
maxLineThree?: number; | ||
} | ||
export interface PopulateConfig extends MaxLineConfig { | ||
scope: HTMLElement | Document; | ||
@@ -20,0 +24,0 @@ titleizePostTown?: boolean; |
@@ -8,18 +8,2 @@ import { capitalisePostTown } from "capitalise-post-town"; | ||
import { cssEscape } from "./util"; | ||
export const updateAddressLines = (targets, address) => { | ||
const [line_1, line_2, line_3] = toAddressLines(numberOfLines(targets), address); | ||
update(targets.line_1, line_1, true); | ||
update(targets.line_2, line_2); | ||
update(targets.line_3, line_3); | ||
}; | ||
export const addressRetrieval = ({ targets, config }) => (address) => { | ||
updateAddressLines(targets, address); | ||
update(targets.post_town, address.post_town); | ||
update(targets.postcode, address.postcode); | ||
updateCountry(targets.country, address); | ||
if (config.populateOrganisation) | ||
update(targets.organisation, address.organisation_name); | ||
if (config.populateCounty) | ||
update(targets.county, address.county); | ||
}; | ||
export const numberOfLines = (targets) => { | ||
@@ -40,8 +24,76 @@ const { line_2, line_3 } = targets; | ||
.join(", "); | ||
export const toAddressLines = (n, address) => { | ||
export const charArrayLength = (arr) => arr.join(" ").trim().length; | ||
const truncate = (line, maxLength) => { | ||
if (line.length <= maxLength) | ||
return [line, ""]; | ||
const words = line.split(" "); | ||
let truncated = ""; | ||
let remaining = ""; | ||
for (let i = 0; i < words.length; i++) { | ||
const word = words[i]; | ||
if (truncated.length + word.length > maxLength) { | ||
remaining = words.slice(i).join(" "); | ||
break; | ||
} | ||
truncated += `${word} `; | ||
} | ||
return [truncated.trim(), remaining.trim()]; | ||
}; | ||
const prependLine = (remaining, nextLine) => { | ||
if (nextLine.length === 0) | ||
return remaining; | ||
return `${remaining}, ${nextLine}`; | ||
}; | ||
export const toMaxLengthLines = (l, options) => { | ||
const { lineCount, maxLineOne, maxLineTwo, maxLineThree } = options; | ||
const result = ["", "", ""]; | ||
const lines = [...l]; | ||
if (maxLineOne) { | ||
const [newLineOne, remaining] = truncate(lines[0], maxLineOne); | ||
result[0] = newLineOne; | ||
if (remaining) | ||
lines[1] = prependLine(remaining, lines[1]); | ||
if (lineCount === 1) | ||
return result; | ||
} | ||
else { | ||
result[0] = lines[0]; | ||
if (lineCount === 1) | ||
return [join(lines), "", ""]; | ||
} | ||
if (maxLineTwo) { | ||
const [newLineTwo, remaining] = truncate(lines[1], maxLineTwo); | ||
result[1] = newLineTwo; | ||
if (remaining) | ||
lines[2] = prependLine(remaining, lines[2]); | ||
if (lineCount === 2) | ||
return result; | ||
} | ||
else { | ||
result[1] = lines[1]; | ||
if (lineCount === 2) | ||
return [result[0], join(lines.slice(1)), ""]; | ||
} | ||
if (maxLineThree) { | ||
const [newLineThree, remaining] = truncate(lines[2], maxLineThree); | ||
result[2] = newLineThree; | ||
if (remaining) | ||
lines[3] = prependLine(remaining, lines[3]); | ||
} | ||
else { | ||
result[2] = lines[2]; | ||
} | ||
return result; | ||
}; | ||
export const toAddressLines = (lineCount, address, options) => { | ||
const { line_1, line_2 } = address; | ||
const line_3 = "line_3" in address ? address.line_3 : ""; | ||
if (n === 3) | ||
if (options.maxLineOne || options.maxLineTwo || options.maxLineThree) | ||
return toMaxLengthLines([line_1, line_2, line_3], { | ||
lineCount, | ||
...options, | ||
}); | ||
if (lineCount === 3) | ||
return [line_1, line_2, line_3]; | ||
if (n === 2) | ||
if (lineCount === 2) | ||
return [line_1, join([line_2, line_3]), ""]; | ||
@@ -63,4 +115,4 @@ return [join([line_1, line_2, line_3]), "", ""]; | ||
}); | ||
const updateLines = (fields, address, scope) => { | ||
const [line_1, line_2, line_3] = toAddressLines(numberOfLines(fields), address); | ||
const updateLines = (fields, address, scope, config) => { | ||
const [line_1, line_2, line_3] = toAddressLines(numberOfLines(fields), address, config); | ||
update(toElem(fields.line_1 || null, scope), line_1); | ||
@@ -133,3 +185,3 @@ update(toElem(fields.line_2 || null, scope), line_2); | ||
} | ||
updateLines(fields, address, scope); | ||
updateLines(fields, address, scope, config); | ||
delete address.line_1; | ||
@@ -136,0 +188,0 @@ delete address.line_2; |
{ | ||
"name": "@ideal-postcodes/jsutil", | ||
"version": "6.2.0", | ||
"version": "6.3.0", | ||
"description": "Browser Address Autocomplete for api.ideal-postcodes.co.uk", | ||
@@ -26,4 +26,2 @@ "author": { | ||
"semantic-release": "semantic-release", | ||
"lint": "eslint lib/**/*.ts", | ||
"lint-fix": "eslint lib/**/*.ts --fix", | ||
"coverage": "codecov", | ||
@@ -112,3 +110,2 @@ "docs": "typedoc", | ||
"@babel/preset-env": "~7.16.5", | ||
"@cablanchard/eslint-config": "~2.1.0", | ||
"@cablanchard/semantic-release": "~1.3.4", | ||
@@ -126,3 +123,2 @@ "@cablanchard/tsconfig": "~2.0.0", | ||
"@types/prettier": "~2.7.0", | ||
"@typescript-eslint/eslint-plugin": "~5.32.0", | ||
"agadoo": "~2.0.0", | ||
@@ -134,4 +130,2 @@ "babel": "~6.23.0", | ||
"dotenv": "~16.0.0", | ||
"eslint": "~7.31.0", | ||
"eslint-plugin-compat": "~4.0.0", | ||
"karma": "~6.3.1", | ||
@@ -142,3 +136,2 @@ "karma-babel-preprocessor": "~8.0.1", | ||
"karma-mocha": "~2.0.1", | ||
"karma-sauce-launcher": "~4.3.3", | ||
"karma-typescript": "~5.5.1", | ||
@@ -149,3 +142,3 @@ "karma-typescript-es6-transform": "~5.5.2", | ||
"prettier": "~2.7.1", | ||
"puppeteer": "~16.1.0", | ||
"puppeteer": "~21.0.3", | ||
"regenerator-runtime": "~0.13.5", | ||
@@ -152,0 +145,0 @@ "semantic-release": "~19.0.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
110135
37
2633