npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-reflection

v1.0.7

Published

Javascript reflection is a library to examine, introspect, and modify javascript code structure and behavior at runtime.

Downloads

2

Readme

ReflectionJS

Javascript reflection is a library to examine, introspect, and modify javascript code structure and behavior at runtime.

Installation

npm install js-reflection

Usage

const reflection = require('js-reflection');

function MyObject(param) {

	this.hello = 'Hello World !';

	this.sub = {
		sayHello: function () {
			return 'Hi People !';
		}
	};
}

MyObject.prototype.test = 'test';

MyObject.prototype.sayHello = function (/* A comment */ param1, /* Another comment */ param2 /* A post comment */,
	// a line comment
	param3,
	param4) {

	return this.hello;
};

const myObject = new MyObject();

console.log('hasProperty', new reflection.Obj(myObject).hasProperty('hello'));
console.log('hasMethod', new reflection.Obj(myObject).hasMethod('sayHello'));
console.log('getName', new reflection.Obj(myObject).getName());
console.log('getConstructor', new reflection.Obj(myObject).getConstructor());
console.log('getConstructorParameters', new reflection.Obj(myObject).getConstructorParameters());
console.log('getMethod', new reflection.Obj(myObject).getMethod('sayHello'));
console.log('getMethods', new reflection.Obj(myObject).getMethods());
console.log('getMethodParameters', new reflection.Obj(myObject).getMethodParameters('sayHello'));
console.log('getProperty', new reflection.Obj(myObject).getProperty('hello'));
console.log('getProperties', new reflection.Obj(myObject).getProperties());

function myFunction(/* A comment */ param1, /* Another comment */ param2 /* A post comment */,
	// a line comment
	param3,
	param4) {}

console.log('getName', new reflection.Func(myFunction).getName());
console.log('getParameters', new reflection.Func(myFunction).getParameters());

Documentation

reflection

Javascript reflection is a library to examine, introspect, and modify javascript code structure and behavior at runtime.

reflection.typeOf(obj) ⇒ String

Get the type name of an object.

Kind: static method of reflection
Returns: String - The type name, eg 'object', 'number', 'null', 'undefined', 'regexp', 'array', 'string', 'boolean', 'function', 'date' or 'error'.

| Param | Type | Description | | --- | --- | --- | | obj | Object | Object to get the type of. |

Example

const reflection = require('js-reflection');

reflection.typeOf(null);				//'null'
reflection.typeOf(undefined);		//'undefined'
reflection.typeOf(/\s/g);			//'regexp'
reflection.typeOf(true);				//'boolean'
reflection.typeOf([]);				//'array'
reflection.typeOf(1);				//'number'
reflection.typeOf('hello');			//'string'
reflection.typeOf(new Date());		//'date'
reflection.typeOf(new Error());		//'error'
reflection.typeOf(function() {});	//'function'

reflection.Obj(obj)

Create a new meta-object to inspect another object.

Kind: static method of reflection

| Param | Type | Description | | --- | --- | --- | | obj | Object | function | Object or function to inspect. |

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);

obj.hasProperty(property, includePrototype) ⇒ Boolean

Check for a given property.

Kind: instance method of Obj
Returns: Boolean - Whether or not the object has the property.

| Param | Type | Description | | --- | --- | --- | | property | string | Property name to check. | | includePrototype | Boolean | True (default) to look up the prototype chain as well, false to only look at direct object. |

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.hasProperty('hello');			//true
reflectionObj.hasProperty('hello', false);	//false
reflectionObj.hasProperty('sayHello');		//false

obj.hasMethod(method, includePrototype) ⇒ Boolean

Check for a given method.

Kind: instance method of Obj
Returns: Boolean - Whether or the not the object has the method.

| Param | Type | Description | | --- | --- | --- | | method | string | Method name to check. | | includePrototype | Boolean | True to look up the prototype chain as well, false to only look at direct object. |

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.hasProperty('sayHello');	//true
reflectionObj.hasProperty('hello');		//false

obj.getName() ⇒ string

Get the name.

Kind: instance method of Obj
Returns: string - Object's name.
Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getName();	//'testObj'

obj.getConstructor() ⇒ function

Get the constructor.

Kind: instance method of Obj
Returns: function - Object's constructor.
Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);

obj.getConstructorParameters() ⇒ Array

Get the constructors parameters.

Kind: instance method of Obj
Returns: Array - Object constructor's parameter names.
Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getConstructor();	// [function]

obj.getMethodParameters(method) ⇒ Array

Get a methods parameters.

Kind: instance method of Obj
Returns: Array - Object method's parameter names.

| Param | Type | Description | | --- | --- | --- | | method | string | Method name. |

Example

const reflection = require('js-reflection');

function testObj(text) { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getConstructorParameters();	// ['text']

obj.getMethods(includePrototype) ⇒ Array

Get all the methods.

Kind: instance method of Obj
Returns: Array - Object method's names.

| Param | Type | Description | | --- | --- | --- | | includePrototype | Boolean | True to look up the prototype chain as well, false to only look at direct object. |

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getMethods();		//['sayHello']
reflectionObj.getMethods(false);	//[]

obj.getMethod(method, includePrototype) ⇒ function

Get a specific method.

Kind: instance method of Obj
Returns: function - The method.

| Param | Type | Description | | --- | --- | --- | | method | string | Method name. | | includePrototype | Boolean | True to look up the prototype chain as well, false to only look at direct object. |

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getMethod('sayHello');	//[function]

obj.getProperties(includePrototype) ⇒ Array

Get all the properties.

Kind: instance method of Obj
Returns: Array - Object properties's names.

| Param | Type | Description | | --- | --- | --- | | includePrototype | Boolean | True to look up the prototype chain as well, false to only look at direct object. |

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getProperties();		//['hello']
reflectionObj.getProperties(false);	//[]

obj.getProperty(property, includePrototype) ⇒ Any

Get a specific property.

Kind: instance method of Obj
Returns: Any - The property.

| Param | Type | Description | | --- | --- | --- | | property | string | Property's name. | | includePrototype | Boolean | True to look up the prototype chain as well, false to only look at direct object. |

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getProperty('hello');	//'test'

obj.getPropertiesAndMethods(includePrototype) ⇒ Array

Get all the properties and methods.

Kind: instance method of Obj
Returns: Array - Object properties's and methods names.

| Param | Type | Description | | --- | --- | --- | | includePrototype | Boolean | True to look up the prototype chain as well, false to only look at direct object. |

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getPropertiesAndMethods();		//['hello', 'sayHello']
reflectionObj.getPropertiesAndMethods(false);	//[]

obj.getPropertyOrMethod(propertyOrMethod, includePrototype) ⇒ Any

Get a specific property or method.

Kind: instance method of Obj
Returns: Any - The property or method.

| Param | Type | Description | | --- | --- | --- | | propertyOrMethod | string | Property or method's name. | | includePrototype | Boolean | True to look up the prototype chain as well, false to only look at direct object. |

Example

const reflection = require('js-reflection');

function testObj() { };
testObj.prototype = { hello: 'test', sayHello: function() {} };

const reflectionObj = new reflection.Obj(testObj);
reflectionObj.getPropertyOrMethod('hello');	//'test'
reflectionObj.getPropertyOrMethod('sayHello');	//'[function]'

reflection.Func(func)

Create a new meta-funct to inspect another object.

Kind: static method of reflection

| Param | Type | Description | | --- | --- | --- | | func | function | Function to inspect. |

Example

const reflection = require('js-reflection');

function testFunc() { };

const reflectionFunc = new reflection.Func(testFunc);

func.getName() ⇒ string

Get the name.

Kind: instance method of Func
Returns: string - Functions's name.
Example

const reflection = require('js-reflection');

function testFunc() { };

const reflectionFunc = new reflection.Func(testFunc);
reflectionFunc.getName();	//'testFunc'

func.getParameters() ⇒ Array

Get the parameters.

Kind: instance method of Func
Returns: Array - Functions's parameter names.
Example

const reflection = require('js-reflection');

function testFunc(text) { };

const reflectionFunc = new reflection.Func(testFunc);
reflectionFunc.getParameters();	//['text']