-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: better typing and easier exports from sfdc * test: more trimTo15 tests * refactor: pr feedback
- Loading branch information
Showing
11 changed files
with
181 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,82 +9,98 @@ import { findKey } from '@salesforce/kit'; | |
import { AnyJson, asJsonMap, isJsonMap, JsonMap, Optional } from '@salesforce/ts-types'; | ||
import { SfdcUrl } from './sfdcUrl'; | ||
|
||
export const sfdc = { | ||
/** | ||
* Converts an 18 character Salesforce ID to 15 characters. | ||
* | ||
* @param id The id to convert. | ||
*/ | ||
trimTo15: (id?: string): Optional<string> => { | ||
if (id?.length && id.length > 15) { | ||
id = id.substring(0, 15); | ||
} | ||
return id; | ||
}, | ||
/** | ||
* Converts an 18 character Salesforce ID to 15 characters. | ||
* | ||
* @param id The id to convert. | ||
*/ | ||
export function trimTo15(id: string): string; | ||
export function trimTo15(id?: undefined): undefined; | ||
export function trimTo15(id: string | undefined): string | undefined; | ||
export function trimTo15(id: string | undefined): string | undefined { | ||
if (!id) { | ||
return undefined; | ||
} | ||
if (id.length && id.length > 15) { | ||
id = id.substring(0, 15); | ||
} | ||
return id; | ||
} | ||
|
||
/** | ||
* Tests whether an API version matches the format `i.0`. | ||
* | ||
* @param value The API version as a string. | ||
*/ | ||
validateApiVersion: (value: string): boolean => value == null || /^[1-9]\d\.0$/.test(value), | ||
/** | ||
* Tests whether an API version matches the format `i.0`. | ||
* | ||
* @param value The API version as a string. | ||
*/ | ||
export const validateApiVersion = (value: string): boolean => value == null || /^[1-9]\d\.0$/.test(value); | ||
|
||
/** | ||
* Tests whether an email matches the format `[email protected]` | ||
* | ||
* @param value The email as a string. | ||
*/ | ||
validateEmail: (value: string): boolean => /^[^.][^@]*@[^.]+(\.[^.\s]+)+$/.test(value), | ||
/** | ||
* Tests whether an email matches the format `[email protected]` | ||
* | ||
* @param value The email as a string. | ||
*/ | ||
export const validateEmail = (value: string): boolean => /^[^.][^@]*@[^.]+(\.[^.\s]+)+$/.test(value); | ||
|
||
/** | ||
* Tests whether a given url is an internal Salesforce domain | ||
* | ||
* @param url | ||
*/ | ||
export const isInternalUrl = (url: string): boolean => new SfdcUrl(url).isInternalUrl(); | ||
|
||
/** | ||
* Tests whether a Salesforce ID is in the correct format, a 15- or 18-character length string with only letters and numbers | ||
* | ||
* @param value The ID as a string. | ||
*/ | ||
validateSalesforceId: (value: string): boolean => | ||
/[a-zA-Z0-9]{18}|[a-zA-Z0-9]{15}/.test(value) && (value.length === 15 || value.length === 18), | ||
/** | ||
* Tests whether a Salesforce ID is in the correct format, a 15- or 18-character length string with only letters and numbers | ||
* | ||
* @param value The ID as a string. | ||
*/ | ||
export const validateSalesforceId = (value: string): boolean => | ||
/[a-zA-Z0-9]{18}|[a-zA-Z0-9]{15}/.test(value) && (value.length === 15 || value.length === 18); | ||
|
||
/** | ||
* Tests whether a path is in the correct format; the value doesn't include the characters "[", "]", "?", "<", ">", "?", "|" | ||
* | ||
* @param value The path as a string. | ||
*/ | ||
validatePathDoesNotContainInvalidChars: (value: string): boolean => | ||
// eslint-disable-next-line no-useless-escape | ||
!/[\["\?<>\|\]]+/.test(value), | ||
/** | ||
* Returns the first key within the object that has an upper case first letter. | ||
* | ||
* @param data The object in which to check key casing. | ||
* @param sectionBlocklist properties in the object to exclude from the search. e.g. a blocklist of `["a"]` and data of `{ "a": { "B" : "b"}}` would ignore `B` because it is in the object value under `a`. | ||
*/ | ||
findUpperCaseKeys: (data?: JsonMap, sectionBlocklist: string[] = []): Optional<string> => { | ||
let key: Optional<string>; | ||
findKey(data, (val: AnyJson, k: string) => { | ||
if (k.substr(0, 1) === k.substr(0, 1).toUpperCase()) { | ||
key = k; | ||
} else if (isJsonMap(val)) { | ||
if (sectionBlocklist.includes(k)) { | ||
return key; | ||
} | ||
key = sfdc.findUpperCaseKeys(asJsonMap(val)); | ||
/** | ||
* Tests whether a path is in the correct format; the value doesn't include the characters "[", "]", "?", "<", ">", "?", "|" | ||
* | ||
* @param value The path as a string. | ||
*/ | ||
export const validatePathDoesNotContainInvalidChars = (value: string): boolean => | ||
// eslint-disable-next-line no-useless-escape | ||
!/[\["\?<>\|\]]+/.test(value); | ||
/** | ||
* Returns the first key within the object that has an upper case first letter. | ||
* | ||
* @param data The object in which to check key casing. | ||
* @param sectionBlocklist properties in the object to exclude from the search. e.g. a blocklist of `["a"]` and data of `{ "a": { "B" : "b"}}` would ignore `B` because it is in the object value under `a`. | ||
*/ | ||
export const findUpperCaseKeys = (data?: JsonMap, sectionBlocklist: string[] = []): Optional<string> => { | ||
let key: Optional<string>; | ||
findKey(data, (val: AnyJson, k: string) => { | ||
if (/^[A-Z]/.test(k)) { | ||
key = k; | ||
} else if (isJsonMap(val)) { | ||
if (sectionBlocklist.includes(k)) { | ||
return key; | ||
} | ||
return key; | ||
}); | ||
key = findUpperCaseKeys(asJsonMap(val)); | ||
} | ||
return key; | ||
}, | ||
}); | ||
return key; | ||
}; | ||
|
||
/** | ||
* Tests whether a given string is an access token | ||
* | ||
* @param value | ||
*/ | ||
matchesAccessToken: (value: string): boolean => /^(00D\w{12,15})![.\w]*$/.test(value), | ||
/** | ||
* Tests whether a given string is an access token | ||
* | ||
* @param value | ||
*/ | ||
export const matchesAccessToken = (value: string): boolean => /^(00D\w{12,15})![.\w]*$/.test(value); | ||
|
||
/** | ||
* Tests whether a given url is an internal Salesforce domain | ||
* | ||
* @param url | ||
*/ | ||
isInternalUrl: (url: string): boolean => new SfdcUrl(url).isInternalUrl(), | ||
/** @deprecated import the individual functions instead of the whole object */ | ||
export const sfdc = { | ||
trimTo15, | ||
validateApiVersion, | ||
validateEmail, | ||
isInternalUrl, | ||
matchesAccessToken, | ||
validateSalesforceId, | ||
validatePathDoesNotContainInvalidChars, | ||
findUpperCaseKeys, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.