-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAgeUnitDefinitions.cpp
More file actions
53 lines (51 loc) · 1.88 KB
/
AgeUnitDefinitions.cpp
File metadata and controls
53 lines (51 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "AgeUnitDefinitions.hpp"
#include <imebra/definitions.h>
#include <stdexcept>
void bindings::AgeUnitDefinitions::Init(v8::Local<v8::Object> exports) {
auto obj = Nan::New<v8::Object>();
Nan::Set(obj,Nan::New("days").ToLocalChecked(),Nan::New("D").ToLocalChecked());
Nan::Set(obj,Nan::New("weeks").ToLocalChecked(),Nan::New("W").ToLocalChecked());
Nan::Set(obj,Nan::New("months").ToLocalChecked(),Nan::New("M").ToLocalChecked());
Nan::Set(obj,Nan::New("years").ToLocalChecked(),Nan::New("Y").ToLocalChecked());
Nan::Set(exports,Nan::New("ageUnit_t").ToLocalChecked(),obj);
}
bool bindings::AgeUnitDefinitions::Decode(v8::Local<v8::Value> jsValue, imebra::ageUnit_t& out) {
if(!jsValue->IsString()) return false;
ssize_t length = Nan::DecodeBytes(jsValue);
std::vector<char> strValue;
strValue.resize(length);
if(Nan::DecodeWrite(strValue.data(),length,jsValue) != length) {
return false;
}
auto value = std::string_view(strValue.data(),length);
if(value == "D") {
out = imebra::ageUnit_t::days;
return true;
}
if(value == "W") {
out = imebra::ageUnit_t::weeks;
return true;
}
if(value == "M") {
out = imebra::ageUnit_t::months;
return true;
}
if(value == "Y") {
out = imebra::ageUnit_t::years;
return true;
}
return false;
}
v8::Local<v8::Value> bindings::AgeUnitDefinitions::Encode(imebra::ageUnit_t value) {
switch(value) {
case imebra::ageUnit_t::days:
return Nan::New("D").ToLocalChecked();
case imebra::ageUnit_t::weeks:
return Nan::New("W").ToLocalChecked();
case imebra::ageUnit_t::months:
return Nan::New("M").ToLocalChecked();
case imebra::ageUnit_t::years:
return Nan::New("Y").ToLocalChecked();
}
throw std::runtime_error("received invalid enum");
}