TagSpaces is an offline, open source, document manager with tagging support
added new helper utils for dates and geo
Ilian Sapundshieva62895921a99
diff --git a/app/utils/dates.js b/app/utils/dates.jsindex de66427d0..510c0c70a 100644--- a/app/utils/dates.js+++ b/app/utils/dates.js@@ -17,7 +17,55 @@* @flow*/-// Format Sun May 11, 2014 to 2014-05+/** Returns true if the input is one of the following formats:+ * 2015, 201402, 20220212, 20190230~12, 20190230~1245, 20190230~124612 */+export function isDateTimeTag(tagDate: string): boolean {+ return (+ isYear(tagDate) ||+ isYearMonth(tagDate) ||+ isYearMonthDay(tagDate) ||+ isYearMonthDayHour(tagDate) ||+ isYearMonthDayHourMin(tagDate) ||+ isYearMonthDayHourMinSec(tagDate)+ );+}++/** Returns true if string is this format: 2015 */+export function isYear(tagDate: string): boolean {+ return /(^|\s)([0123][0123456789][0123456789][0123456789])(\s|$)/.test(tagDate);+}++/** Returns true if string is this format: 201512 */+export function isYearMonth(tagDate: string): boolean {+ return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789])(\s|$)/.test(tagDate);+}++/** Returns true if string is this format: 20151223 */+export function isYearMonthDay(tagDate: string): boolean {+ return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789])(\s|$)/.test(tagDate);+}++/** Returns true if string is this format: 20151223~01 */+export function isYearMonthDayHour(tagDate: string): boolean {+ return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789]~[0123456][0123456789])(\s|$)/.test(tagDate);+}++/** Returns true if string is this format: 20151223~0112 */+export function isYearMonthDayHourMin(tagDate: string): boolean {+ return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789]~[0123456][0123456789][0123456][0123456789])(\s|$)/.test(tagDate);+}++/** Returns true if string is this format: 20151223~011358 */+export function isYearMonthDayHourMinSec(tagDate: string): boolean {+ return /(^|\s)([0123][0123456789][0123456789][0123456789][01][0123456789][0123][0123456789]~[0123456][0123456789][0123456][0123456789][0123456][0123456789])(\s|$)/.test(tagDate);+}++/** Convert a date in this 2013-01-02 12:23:58 format */+export function formatDateTime(date: Date): string {+ return date.toISOString().substring(0, 19).split('T').join(' ');+}++/** Converts 'Sun May 11, 2014' to 2014-05 */export function parseDateMonth(date) {const d = new Date(date);let month = '' + (d.getMonth() + 1);@@ -29,7 +77,7 @@ export function parseDateMonth(date) {return [year, month].join('');}-// Format Sun May 11, 2014 to 2014-05-11+/** Converts 'Sun May 11, 2014' to 2014-05-11 */export function parseDate(date) {const d = new Date(date);let month = '' + (d.getMonth() + 1);diff --git a/app/utils/misc.js b/app/utils/misc.jsindex bcc857bad..4e6a1f732 100644--- a/app/utils/misc.js+++ b/app/utils/misc.js@@ -1,3 +1,5 @@+import { isDate } from "util";+/*** TagSpaces - universal file and folder organizer* Copyright (C) 2017-present TagSpaces UG (haftungsbeschraenkt)@@ -17,6 +19,28 @@* @flow*/+/** Returns true is a string is plus code e.g. 8FWH4HVG+3V 8FWH4HVG+ 8FWH4H+ */+export function isPlusCode(plusCode: string): boolean {+ return /(^|\s)([23456789C][23456789CFGHJMPQRV][23456789CFGHJMPQRVWX]{6}\+[23456789CFGHJMPQRVWX]{2,3})(\s|$)/.test(plusCode);+}++export function parseLatLon(latLongInput: string): {lat: number, lon: number} | false {+ const cleanedInput = latLongInput.replace(/\s+/g, ''); // cleaning spaces+ if (!/^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/.test(cleanedInput)) {+ return false;+ }+ const latLongArray = cleanedInput.split(',');+ const lat = parseFloat(latLongArray[0]);+ const lon = parseFloat(latLongArray[1]);+ if (!isNaN(lat) && !isNaN(lon)) {+ return {+ lat,+ lon+ };+ }+ return false;+}+export function traverse(objArr, func) {objArr.forEach((obj) => {Object.keys(obj).map((objKey) => {