nonenumerable
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,1 +0,1 @@ | ||
export declare function nonenumerable(target: any, key: string): void; | ||
export declare function nonenumerable(proto: any, key: string): void; |
"use strict"; | ||
function nonenumerable(target, key) { | ||
var value = target[key]; | ||
function nonenumerable(proto, key) { | ||
var priv = "$" + key; | ||
var getter = function () { | ||
return value; | ||
return this[priv]; | ||
}; | ||
var setter = function (newVal) { | ||
value = newVal; | ||
if (!this.hasOwnProperty(priv)) { | ||
Object.defineProperty(this, priv, { | ||
value: newVal, | ||
enumerable: false, | ||
writable: true | ||
}); | ||
} | ||
this[priv] = newVal; | ||
}; | ||
if (delete target[key]) { | ||
Object.defineProperty(target, key, { | ||
if (delete proto[key]) { | ||
Object.defineProperty(proto, key, { | ||
get: getter, | ||
@@ -13,0 +20,0 @@ set: setter, |
{ | ||
"name": "nonenumerable", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Decorator to make properties non-enumerable.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -1,14 +0,21 @@ | ||
export function nonenumerable(target: any, key: string) { | ||
var value = target[ key ]; | ||
export function nonenumerable(proto: any, key: string) { | ||
const priv = `$${key}`; | ||
var getter = function () { | ||
return value; | ||
return this[priv]; | ||
}; | ||
var setter = function (newVal: any) { | ||
value = newVal; | ||
if (!this.hasOwnProperty(priv)) { | ||
Object.defineProperty(this, priv, { | ||
value: newVal, | ||
enumerable: false, | ||
writable: true | ||
}); | ||
} | ||
this[priv] = newVal; | ||
}; | ||
if (delete target[key]) { | ||
Object.defineProperty(target, key, { | ||
if (delete proto[ key ]) { | ||
Object.defineProperty(proto, key, { | ||
get: getter, | ||
@@ -15,0 +22,0 @@ set: setter, |
@@ -9,3 +9,3 @@ import { assert } from "chai"; | ||
@nonenumerable | ||
private c = 3; | ||
c = 3; | ||
@@ -31,2 +31,8 @@ constructor () { | ||
it("should support multiple instances", () => { | ||
let secondInstance = new NonEnumerable(); | ||
secondInstance.c = 10; | ||
assert.notEqual(secondInstance.c, nonEnumerable.c); | ||
}); | ||
}); |
4835
90