diff --git a/src/constants.js b/src/constants.js index 6382690..1c29ef3 100644 --- a/src/constants.js +++ b/src/constants.js @@ -1,3 +1,6 @@ +import { getTimestampInSeconds } from "./helpers"; + +export const MIGRATE_VERSION = '2.0.0'; export const DEFAULT_DICTIONARY = { name: 'New', specification: 'Dictionary', @@ -47,7 +50,8 @@ export const DEFAULT_DICTIONARY = { isPublic: false, }, lastUpdated: null, - createdOn: 0, + createdOn: getTimestampInSeconds(), + version: MIGRATE_VERSION, }; export const DEFAULT_PAGE_SIZE = 50; diff --git a/src/helpers.js b/src/helpers.js index e1a13be..c85e288 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -4,6 +4,28 @@ export function cloneObject(object) { return JSON.parse(JSON.stringify(object)); } +export function getIndicesOf(searchStr, findIn, caseSensitive) { + // https://stackoverflow.com/a/3410557 + const searchStrLen = searchStr.length; + if (searchStrLen == 0) { + return []; + } + let startIndex = 0, index, indices = []; + if (!caseSensitive) { + findIn = findIn.toLowerCase(); + searchStr = searchStr.toLowerCase(); + } + while ((index = findIn.indexOf(searchStr, startIndex)) > -1) { + indices.push(index); + startIndex = index + searchStrLen; + } + return indices; +} + +export function getTimestampInSeconds() { + return Math.round(Date.now() / 1000); +} + export function removeTags(html) { if (html) { var tagBody = '(?:[^"\'>]|"[^"]*"|\'[^\']*\')*'; @@ -32,21 +54,3 @@ export function removeTags(html) { export function slugify(string) { return removeDiacritics(string).replace(/[!a-zA-Z0-9-_]/g, '-'); } - -export function getIndicesOf(searchStr, findIn, caseSensitive) { - // https://stackoverflow.com/a/3410557 - const searchStrLen = searchStr.length; - if (searchStrLen == 0) { - return []; - } - let startIndex = 0, index, indices = []; - if (!caseSensitive) { - findIn = findIn.toLowerCase(); - searchStr = searchStr.toLowerCase(); - } - while ((index = findIn.indexOf(searchStr, startIndex)) > -1) { - indices.push(index); - startIndex = index + searchStrLen; - } - return indices; -} diff --git a/src/js/dictionaryManagement.js b/src/js/dictionaryManagement.js index c49b77f..732e0f6 100644 --- a/src/js/dictionaryManagement.js +++ b/src/js/dictionaryManagement.js @@ -1,5 +1,5 @@ import { renderDictionaryDetails, renderPartsOfSpeech } from "./render"; -import { removeTags, cloneObject } from "../helpers"; +import { removeTags, cloneObject, getTimestampInSeconds } from "../helpers"; import { LOCAL_STORAGE_KEY, DEFAULT_DICTIONARY } from "../constants"; export function updateDictionary () { @@ -73,6 +73,7 @@ export function saveAndCloseEditModal() { } export function saveDictionary() { + window.currentDictionary.lastUpdated = getTimestampInSeconds(); window.localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(window.currentDictionary)); }