Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ObjectId timestamp parser operation #987

Merged
merged 1 commit into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@
"Escape string",
"Unescape string",
"Pseudo-Random Number Generator",
"Sleep"
"Sleep",
"Parse ObjectId timestamp"
]
},
{
Expand Down
47 changes: 47 additions & 0 deletions src/core/operations/ParseObjectIdTimestamp.mjs
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion tests/operations/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -120,4 +121,3 @@ const logOpsTestReport = logTestReport.bind(null, testStatus);
const results = await TestRegister.runTests();
logOpsTestReport(results);
})();

24 changes: 24 additions & 0 deletions tests/operations/tests/ParseObjectIdTimestamp.mjs
Original file line number Diff line number Diff line change
@@ -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: [],
}
],
}
]);