# nnu

> Node.js Native Utilities.

Latest version **0.2.0** (published 2017-07-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install nnu
pnpm add nnu
yarn add nnu
bun add nnu
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2017-07-26 |
| First published | 2014-11-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | talrasha007 |

## Links

- npm: https://www.npmjs.com/package/nnu
- Repository: https://github.com/talrasha007/nnu
- Homepage: https://github.com/talrasha007/nnu#readme
- Issues: https://github.com/talrasha007/nnu/issues
- npm.io page: https://npm.io/package/nnu

## Recent versions

- 0.2.0 (latest) — 2017-07-26
- 0.1.7 — 2017-07-13
- 0.1.6 — 2017-07-11
- 0.1.5 — 2017-07-06
- 0.1.4 — 2017-07-06
- 0.1.3 — 2017-07-06
- 0.1.2 — 2017-07-06
- 0.1.1 — 2017-07-05
- 0.1.0 — 2017-07-05
- 0.0.1 — 2014-11-06

## README

# nnu - Node.js Native Utilities
  Some useful utilities for writing node.js native module.
  
## Usage
  Simply add **NAN** and **NNU** as a dependency in the package.json of your Node addon:
```bash
npm install --save nan nnu
```
  Pull in the path to **NAN** and **NNU** in your binding.gyp so that you can use #include <nan.h> in your .cpp files:
```python
"include_dirs" : [
    "<!(node -e \"require('nan')\")",
    "<!(node -e \"require('nnu')\")"
]
```

## Example
  - nnu::setPrivate & nnu::getPrivate
  - nnu::ClassWrap
```c++
#include <iostream>
#include <nnu.h>

// nnu::ClassWrap will simplify the way you write a class.
class SampleClass : public nnu::ClassWrap<SampleClass> {
public:
    static const char * const CLASS_NAME;

    // Static member *cotr* is *required*
    static NAN_METHOD(ctor) {
        int v = 0;
        if (info.Length() > 0) {
            v = info[0]->Int32Value();
        }

        SampleClass *sc = new SampleClass(v);
        sc->Wrap(info.This());

        nnu::setPrivate(info.This(), "_priv_2_", 2);
        nnu::setPrivate(info.This(), "_priv_3_", sc);

        info.GetReturnValue().Set(info.This());
    }

    // Static member *setupMember*  is *required*
    static void setupMember(v8::Local<v8::FunctionTemplate>& tpl) {
        // Use wrapFunction to wrap member function as static.
        Nan::SetPrototypeMethod(tpl, "printPriv", wrapFunction<&SampleClass::printPriv>);
        Nan::SetPrototypeMethod(tpl, "getVal", wrapFunction<&SampleClass::getVal>);
        Nan::SetPrototypeMethod(tpl, "incVal", wrapFunction<&SampleClass::incVal>);
        Nan::SetPrototypeMethod(tpl, "clone", wrapFunction<&SampleClass::clone>);
    }

private:
    SampleClass() : _val(0) { }

    // You can now write member function instead of static, ObjectWrap::Unwrap is no longer needed.
    NAN_METHOD(printPriv) {
        int priv1 = nnu::getPrivate<int>(info.This(), "_priv_2_");
        std::cout << "priv1: " << priv1 << std::endl;

        SampleClass *priv2 = nnu::getPrivate<SampleClass*>(info.This(), "_priv_3_");
        std::cout << "priv2: " << priv2 << " should be equal to " << this << std::endl;
    }

    NAN_METHOD(getVal) {
        info.GetReturnValue().Set(Nan::New(_val));
    }

    NAN_METHOD(incVal) {
        _val++; 
        info.GetReturnValue().Set(info.This());
    }

    NAN_METHOD(clone) {
        v8::Local<v8::Value> args[] = { Nan::New(_val) };
        v8::Local<v8::Object> ret = SampleClass::newInstance(1, args);
        info.GetReturnValue().Set(ret);
    }

private:
    int _val;
};

const char * const SampleClass::CLASS_NAME = "SampleClass";

// newInstance()
// or
// newInstance(int argc, v8::Local<v8::Value> argv[])
NAN_METHOD(newSample) {
    info.GetReturnValue().Set(SampleClass::newInstance());
}

NAN_MODULE_INIT(InitAll) {
    SampleClass::setup(target);

    NAN_EXPORT(target, newSample);
}

NODE_MODULE(nnu_example, InitAll);
```

---
_Source: https://npm.io/package/nnu · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
