-
Notifications
You must be signed in to change notification settings - Fork 465
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR-URL: #497 Reviewed-By: NickNaso <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Gabriel Schulhof <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
- Loading branch information
1 parent
7b1ee96
commit 6192e70
Showing
11 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Date | ||
|
||
`Napi::Date` class is a representation of the JavaScript `Date` object. The | ||
`Napi::Date` class inherits its behavior from `Napi::Value` class | ||
(for more info see [`Napi::Value`](value.md)) | ||
|
||
## Methods | ||
|
||
### Constructor | ||
|
||
Creates a new _empty_ instance of a `Napi::Date` object. | ||
|
||
```cpp | ||
Napi::Date::Date(); | ||
``` | ||
|
||
Creates a new _non-empty_ instance of a `Napi::Date` object. | ||
|
||
```cpp | ||
Napi::Date::Date(napi_env env, napi_value value); | ||
``` | ||
- `[in] env`: The environment in which to construct the `Napi::Date` object. | ||
- `[in] value`: The `napi_value` which is a handle for a JavaScript `Date`. | ||
### New | ||
Creates a new instance of a `Napi::Date` object. | ||
```cpp | ||
static Napi::Date Napi::Date::New(Napi::Env env, double value); | ||
``` | ||
|
||
- `[in] env`: The environment in which to construct the `Napi::Date` object. | ||
- `[in] value`: The time value the JavaScript `Date` will contain represented | ||
as the number of milliseconds since 1 January 1970 00:00:00 UTC. | ||
|
||
Returns a new instance of `Napi::Date` object. | ||
|
||
### ValueOf | ||
|
||
```cpp | ||
double Napi::Date::ValueOf() const; | ||
``` | ||
|
||
Returns the time value as `double` primitive represented as the number of | ||
milliseconds since 1 January 1970 00:00:00 UTC. | ||
|
||
## Operators | ||
|
||
### operator double | ||
|
||
Converts a `Napi::Date` value to a `double` primitive. | ||
|
||
```cpp | ||
Napi::Date::operator double() const; | ||
``` | ||
|
||
### Example | ||
|
||
The following shows an example of casting a `Napi::Date` value to a `double` | ||
primitive. | ||
|
||
```cpp | ||
double operatorVal = Napi::Date::New(Env(), 0); // Napi::Date to double | ||
// or | ||
auto instanceVal = info[0].As<Napi::Date>().ValueOf(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#define NAPI_EXPERIMENTAL | ||
#include "napi.h" | ||
|
||
using namespace Napi; | ||
|
||
#if (NAPI_VERSION > 4) | ||
namespace { | ||
|
||
Value CreateDate(const CallbackInfo& info) { | ||
double input = info[0].As<Number>().DoubleValue(); | ||
|
||
return Date::New(info.Env(), input); | ||
} | ||
|
||
Value IsDate(const CallbackInfo& info) { | ||
Date input = info[0].As<Date>(); | ||
|
||
return Boolean::New(info.Env(), input.IsDate()); | ||
} | ||
|
||
Value ValueOf(const CallbackInfo& info) { | ||
Date input = info[0].As<Date>(); | ||
|
||
return Number::New(info.Env(), input.ValueOf()); | ||
} | ||
|
||
Value OperatorValue(const CallbackInfo& info) { | ||
Date input = info[0].As<Date>(); | ||
|
||
return Boolean::New(info.Env(), input.ValueOf() == static_cast<double>(input)); | ||
} | ||
|
||
} // anonymous namespace | ||
|
||
Object InitDate(Env env) { | ||
Object exports = Object::New(env); | ||
exports["CreateDate"] = Function::New(env, CreateDate); | ||
exports["IsDate"] = Function::New(env, IsDate); | ||
exports["ValueOf"] = Function::New(env, ValueOf); | ||
exports["OperatorValue"] = Function::New(env, OperatorValue); | ||
|
||
return exports; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use strict'; | ||
|
||
const buildType = process.config.target_defaults.default_configuration; | ||
const assert = require('assert'); | ||
|
||
test(require(`./build/${buildType}/binding.node`)); | ||
test(require(`./build/${buildType}/binding_noexcept.node`)); | ||
|
||
function test(binding) { | ||
const { | ||
CreateDate, | ||
IsDate, | ||
ValueOf, | ||
OperatorValue, | ||
} = binding.date; | ||
assert.deepStrictEqual(CreateDate(0), new Date(0)); | ||
assert.strictEqual(IsDate(new Date(0)), true); | ||
assert.strictEqual(ValueOf(new Date(42)), 42); | ||
assert.strictEqual(OperatorValue(new Date(42)), true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters