From 9ed541dca7e6d79f068eb535750093cf7e57ed1f Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Wed, 12 Jun 2019 23:23:37 +0200 Subject: [PATCH] Removed String-Date and its format on methods that uses dates for native Date object --- src/pass.ts | 20 +++--- src/utils.ts | 185 +++++++++++++++++++++++++-------------------------- 2 files changed, 101 insertions(+), 104 deletions(-) diff --git a/src/pass.ts b/src/pass.ts index af6d8f2..b28eaed 100644 --- a/src/pass.ts +++ b/src/pass.ts @@ -183,17 +183,16 @@ export class Pass implements PassIndexSignature { * Sets expirationDate property to the W3C date * * @method expiration - * @params {String} date - the date in string - * @params {String} format - a custom format for the date + * @params date * @returns {this} */ - expiration(date: string | Date, format?: string) { - if (typeof date !== "string" && !(date instanceof Date)) { + expiration(date: Date) { + if (!(date instanceof Date)) { return this; } - let dateParse = dateToW3CString(date, format); + const dateParse = dateToW3CString(date); if (!dateParse) { genericDebug(formatMessage("DATE_FORMAT_UNMATCH", "Expiration date")); @@ -221,13 +220,12 @@ export class Pass implements PassIndexSignature { * Checks and sets data for "beacons", "locations", "maxDistance" and "relevantDate" keys * * @method relevance - * @params {String} type - one of the key above - * @params {Any[]} data - the data to be pushed to the property - * @params {String} [relevanceDateFormat] - A custom format for the date + * @params type - one of the key above + * @params data - the data to be pushed to the property * @return {Number} The quantity of data pushed */ - relevance(type: string, data: any, relevanceDateFormat?: string) { + relevance(type: string, data: any) { let types = ["beacons", "locations", "maxDistance", "relevantDate"]; if (!type || !data || !types.includes(type)) { @@ -257,12 +255,12 @@ export class Pass implements PassIndexSignature { return assignLength(Number(!cond), this); } else if (type === "relevantDate") { - if (typeof data !== "string" && !(data instanceof Date)) { + if (!(data instanceof Date)) { genericDebug(formatMessage("DATE_FORMAT_UNMATCH", "Relevant Date")); return this; } - let dateParse = dateToW3CString(data, relevanceDateFormat); + let dateParse = dateToW3CString(data); if (!dateParse) { genericDebug(formatMessage("DATE_FORMAT_UNMATCH", "Relevant Date")); diff --git a/src/utils.ts b/src/utils.ts index 77f1243..2784756 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,93 +1,92 @@ -import moment from "moment"; -import { EOL } from "os"; - -/** - * Checks if an rgb value is compliant with CSS-like syntax - * - * @function isValidRGB - * @params {String} value - string to analyze - * @returns {Boolean} True if valid rgb, false otherwise - */ - -export function isValidRGB(value: string): boolean { - if (!value || typeof value !== "string") { - return false; - } - - const rgb = value.match(/^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/); - - if (!rgb) { - return false; - } - - return rgb.slice(1, 4).every(v => Math.abs(Number(v)) <= 255); -} - -/** - * Converts a date to W3C Standard format - * - * @function dateToW3Cstring - * @params {String} date - The date to be parsed - * @params {String} [format] - a custom format - * @returns {String|undefined} The parsed string if the parameter is valid, - * undefined otherwise - */ - -export function dateToW3CString(date: string | Date, format?: string) { - if (typeof date !== "string" && !(date instanceof Date)) { - return ""; - } - - const parsedDate = date instanceof Date ? moment(date).format() : moment(date.replace(/\//g, "-"), format || ["MM-DD-YYYY hh:mm:ss", "DD-MM-YYYY hh:mm:ss"]).format(); - - if (parsedDate === "Invalid date") { - return undefined; - } - - return parsedDate; -} - -/** - * Apply a filter to arg0 to remove hidden files names (starting with dot) - * - * @function removeHidden - * @params {String[]} from - list of file names - * @return {String[]} - */ - -export function removeHidden(from: Array): Array { - return from.filter(e => e.charAt(0) !== "."); -} - -/** - * Creates a buffer of translations in Apple .strings format - * - * @function generateStringFile - * @params {Object} lang - structure containing related to ISO 3166 alpha-2 code for the language - * @returns {Buffer} Buffer to be written in pass.strings for language in lang - * @see https://apple.co/2M9LWVu - String Resources - */ - -export function generateStringFile(lang: { [index: string]: string }): Buffer { - if (!Object.keys(lang).length) { - return Buffer.from("", "utf8"); - } - - // Pass.strings format is the following one for each row: - // "key" = "value"; - - const strings = Object.keys(lang) - .map(key => `"${key}" = "${lang[key].replace(/"/g, '\"')}";`); - - return Buffer.from(strings.join(EOL), "utf8"); -} - -/** - * Creates a new object with custom length property - * @param {number} value - the length - * @param {Array>} source - the main sources of properties - */ - -export function assignLength(length: number, ...sources: Array<{ [key: string]: any }>): Array<{ [key: string]: any }> { - return Object.assign({ length }, ...sources); -} +import moment from "moment"; +import { EOL } from "os"; + +/** + * Checks if an rgb value is compliant with CSS-like syntax + * + * @function isValidRGB + * @params {String} value - string to analyze + * @returns {Boolean} True if valid rgb, false otherwise + */ + +export function isValidRGB(value: string): boolean { + if (!value || typeof value !== "string") { + return false; + } + + const rgb = value.match(/^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/); + + if (!rgb) { + return false; + } + + return rgb.slice(1, 4).every(v => Math.abs(Number(v)) <= 255); +} + +/** + * Converts a date to W3C Standard format + * + * @function dateToW3Cstring + * @params date - The date to be parsed + * @returns - The parsed string if the parameter is valid, + * undefined otherwise + */ + +export function dateToW3CString(date: Date) { + if (!(date instanceof Date)) { + return ""; + } + + const parsedDate = moment(date).format(); + + if (parsedDate === "Invalid date") { + return undefined; + } + + return parsedDate; +} + +/** + * Apply a filter to arg0 to remove hidden files names (starting with dot) + * + * @function removeHidden + * @params {String[]} from - list of file names + * @return {String[]} + */ + +export function removeHidden(from: Array): Array { + return from.filter(e => e.charAt(0) !== "."); +} + +/** + * Creates a buffer of translations in Apple .strings format + * + * @function generateStringFile + * @params {Object} lang - structure containing related to ISO 3166 alpha-2 code for the language + * @returns {Buffer} Buffer to be written in pass.strings for language in lang + * @see https://apple.co/2M9LWVu - String Resources + */ + +export function generateStringFile(lang: { [index: string]: string }): Buffer { + if (!Object.keys(lang).length) { + return Buffer.from("", "utf8"); + } + + // Pass.strings format is the following one for each row: + // "key" = "value"; + + const strings = Object.keys(lang) + .map(key => `"${key}" = "${lang[key].replace(/"/g, '\"')}";`); + + return Buffer.from(strings.join(EOL), "utf8"); +} + +/** + * Creates a new object with custom length property + * @param {number} value - the length + * @param {Array>} source - the main sources of properties + */ + +export function assignLength(length: number, ...sources: Array<{ [key: string]: any }>): Array<{ [key: string]: any }> { + return Object.assign({ length }, ...sources); +}