From 9f4ef9cdad35161cc8982c8c0f5b3a5600b6dd0b Mon Sep 17 00:00:00 2001 From: Dominic Fitch-Jones Date: Sat, 21 Mar 2020 17:42:17 -0400 Subject: [PATCH 1/2] Add ObjectId timestamp parser operation --- src/core/config/Categories.json | 3 +- .../operations/ParseObjectIdTimestamp.mjs | 47 +++++++++++++++++++ tests/operations/index.mjs | 2 +- .../tests/ParseObjectIdTimestamp.mjs | 24 ++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/core/operations/ParseObjectIdTimestamp.mjs create mode 100644 tests/operations/tests/ParseObjectIdTimestamp.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 5957c8b7..a05ca095 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -247,7 +247,8 @@ "Escape string", "Unescape string", "Pseudo-Random Number Generator", - "Sleep" + "Sleep", + "Parse ObjectId timestamp" ] }, { diff --git a/src/core/operations/ParseObjectIdTimestamp.mjs b/src/core/operations/ParseObjectIdTimestamp.mjs new file mode 100644 index 00000000..bb0f68ed --- /dev/null +++ b/src/core/operations/ParseObjectIdTimestamp.mjs @@ -0,0 +1,47 @@ +/** + * @author dmfj [dominic@dmfj.io] + * @copyright Crown Copyright 2020 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; +import BSON from "bson"; + +/** + * Parse ObjectId timestamp operation + */ +class ParseObjectIdTimestamp extends Operation { + + /** + * ParseObjectIdTimestamp constructor + */ + constructor() { + super(); + + this.name = "Parse ObjectId timestamp"; + this.module = "Default"; + this.description = "Parse timestamp from MongoDB/BSON ObjectId hex string."; + this.infoURL = "https://docs.mongodb.com/manual/reference/method/ObjectId.getTimestamp/"; + this.inputType = "string"; + this.outputType = "string"; + this.args = []; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + try { + const objectId = new BSON.ObjectID(input); + return objectId.getTimestamp().toISOString(); + } catch (err) { + throw new OperationError(err); + } + } + +} + +export default ParseObjectIdTimestamp; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 7e5ef374..a83fddc8 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -100,6 +100,7 @@ import "./tests/Lorenz.mjs"; import "./tests/LuhnChecksum.mjs"; import "./tests/CipherSaber2.mjs"; import "./tests/Colossus.mjs"; +import "./tests/ParseObjectIdTimestamp.mjs"; // Cannot test operations that use the File type yet @@ -120,4 +121,3 @@ const logOpsTestReport = logTestReport.bind(null, testStatus); const results = await TestRegister.runTests(); logOpsTestReport(results); })(); - diff --git a/tests/operations/tests/ParseObjectIdTimestamp.mjs b/tests/operations/tests/ParseObjectIdTimestamp.mjs new file mode 100644 index 00000000..f2c919a5 --- /dev/null +++ b/tests/operations/tests/ParseObjectIdTimestamp.mjs @@ -0,0 +1,24 @@ +/** + * Parse ObjectId timestamp tests + * + * @author dmfj [dominic@dmfj.io] + * + * @copyright Crown Copyright 2018 + * @license Apache-2.0 + */ +import TestRegister from "../../lib/TestRegister.mjs"; + + +TestRegister.addTests([ + { + name: "Parse ISO timestamp from ObjectId", + input: "000000000000000000000000", + expectedOutput: "1970-01-01T00:00:00.000Z", + recipeConfig: [ + { + op: "Parse ObjectId timestamp", + args: [], + } + ], + } +]); From 3a0c8a199a03a187795b09939e613f74660d7b5d Mon Sep 17 00:00:00 2001 From: n1474335 Date: Fri, 27 Mar 2020 11:56:42 +0000 Subject: [PATCH 2/2] Tidied up 'Parse ObjectID Timestamp' operation --- src/core/config/Categories.json | 4 ++-- src/core/operations/ParseObjectIdTimestamp.mjs | 14 +++++++------- tests/operations/index.mjs | 2 +- tests/operations/tests/ParseObjectIdTimestamp.mjs | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index a05ca095..77e3d319 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -242,13 +242,13 @@ "Convert co-ordinate format", "Show on map", "Parse UNIX file permissions", + "Parse ObjectID timestamp", "Swap endianness", "Parse colour code", "Escape string", "Unescape string", "Pseudo-Random Number Generator", - "Sleep", - "Parse ObjectId timestamp" + "Sleep" ] }, { diff --git a/src/core/operations/ParseObjectIdTimestamp.mjs b/src/core/operations/ParseObjectIdTimestamp.mjs index bb0f68ed..f86c098e 100644 --- a/src/core/operations/ParseObjectIdTimestamp.mjs +++ b/src/core/operations/ParseObjectIdTimestamp.mjs @@ -9,19 +9,19 @@ import OperationError from "../errors/OperationError.mjs"; import BSON from "bson"; /** - * Parse ObjectId timestamp operation + * Parse ObjectID timestamp operation */ -class ParseObjectIdTimestamp extends Operation { +class ParseObjectIDTimestamp extends Operation { /** - * ParseObjectIdTimestamp constructor + * ParseObjectIDTimestamp constructor */ constructor() { super(); - this.name = "Parse ObjectId timestamp"; - this.module = "Default"; - this.description = "Parse timestamp from MongoDB/BSON ObjectId hex string."; + this.name = "Parse ObjectID timestamp"; + this.module = "Serialise"; + this.description = "Parse timestamp from MongoDB/BSON ObjectID hex string."; this.infoURL = "https://docs.mongodb.com/manual/reference/method/ObjectId.getTimestamp/"; this.inputType = "string"; this.outputType = "string"; @@ -44,4 +44,4 @@ class ParseObjectIdTimestamp extends Operation { } -export default ParseObjectIdTimestamp; +export default ParseObjectIDTimestamp; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index a83fddc8..8d3cd623 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -100,7 +100,7 @@ import "./tests/Lorenz.mjs"; import "./tests/LuhnChecksum.mjs"; import "./tests/CipherSaber2.mjs"; import "./tests/Colossus.mjs"; -import "./tests/ParseObjectIdTimestamp.mjs"; +import "./tests/ParseObjectIDTimestamp.mjs"; // Cannot test operations that use the File type yet diff --git a/tests/operations/tests/ParseObjectIdTimestamp.mjs b/tests/operations/tests/ParseObjectIdTimestamp.mjs index f2c919a5..7a28f62e 100644 --- a/tests/operations/tests/ParseObjectIdTimestamp.mjs +++ b/tests/operations/tests/ParseObjectIdTimestamp.mjs @@ -1,5 +1,5 @@ /** - * Parse ObjectId timestamp tests + * Parse ObjectID timestamp tests * * @author dmfj [dominic@dmfj.io] * @@ -16,7 +16,7 @@ TestRegister.addTests([ expectedOutput: "1970-01-01T00:00:00.000Z", recipeConfig: [ { - op: "Parse ObjectId timestamp", + op: "Parse ObjectID timestamp", args: [], } ],