Add ObjectId timestamp parser operation
This commit is contained in:
parent
26b19350f2
commit
9f4ef9cdad
@ -247,7 +247,8 @@
|
|||||||
"Escape string",
|
"Escape string",
|
||||||
"Unescape string",
|
"Unescape string",
|
||||||
"Pseudo-Random Number Generator",
|
"Pseudo-Random Number Generator",
|
||||||
"Sleep"
|
"Sleep",
|
||||||
|
"Parse ObjectId timestamp"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
47
src/core/operations/ParseObjectIdTimestamp.mjs
Normal file
47
src/core/operations/ParseObjectIdTimestamp.mjs
Normal file
@ -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;
|
@ -100,6 +100,7 @@ import "./tests/Lorenz.mjs";
|
|||||||
import "./tests/LuhnChecksum.mjs";
|
import "./tests/LuhnChecksum.mjs";
|
||||||
import "./tests/CipherSaber2.mjs";
|
import "./tests/CipherSaber2.mjs";
|
||||||
import "./tests/Colossus.mjs";
|
import "./tests/Colossus.mjs";
|
||||||
|
import "./tests/ParseObjectIdTimestamp.mjs";
|
||||||
|
|
||||||
|
|
||||||
// Cannot test operations that use the File type yet
|
// Cannot test operations that use the File type yet
|
||||||
@ -120,4 +121,3 @@ const logOpsTestReport = logTestReport.bind(null, testStatus);
|
|||||||
const results = await TestRegister.runTests();
|
const results = await TestRegister.runTests();
|
||||||
logOpsTestReport(results);
|
logOpsTestReport(results);
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
24
tests/operations/tests/ParseObjectIdTimestamp.mjs
Normal file
24
tests/operations/tests/ParseObjectIdTimestamp.mjs
Normal file
@ -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: [],
|
||||||
|
}
|
||||||
|
],
|
||||||
|
}
|
||||||
|
]);
|
Loading…
Reference in New Issue
Block a user