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

object-property-names

v0.1.9

Published

Get any property names you need from a JavaScript object.

Downloads

7

Readme

object-property-names

npm version Build Status JavaScript Style Guide Dependency Status

Get any property names (enumerable or not) you need from a JavaScript object.

Description

The properties will be found upon the object through its prototype chain to the top. During the prototype chain lookup, two rules - the type rule and the property rule, can apply to ignore the specified types and properties.

Features

  • Tiny and No dependence
  • Flexible control
  • Support ES2015 Module and UMD (support jsnext:main)

Type Rule & Property Rule

The Type Rule is the list of the types to be ignored and the properties of those ignored types won't be returned. Please note that the type rule applies not only to the prototype but also to the given object.

The types can be native or custom, such as String, Function, Array and your SomeClass. During the prototype chain lookup, any of the types is found ignored then its upper prototype chain won't be sought. By default all the native types will be ignored.

The Property Rule is the list of the properties to be ignored.

Those properties may be private, context-irrelative or non-enumerable. By default the property name prefixed with _ will be regarded private and so ignored. The constructor property is also ignored.

API

getPropertyNamesByRules(object[, rules])

  • the parameter object whose enumerable and non-enumerable property names to be returned
  • the optional parameter rules to create the type/property rule
rules = {
  ignoredTypes: array(string|regexp) | string | regexp | function,
  ignoredProperties: array(string|regexp) | string | regexp | function
}

The rules can also be any value that coerces to a Boolean except for an Object. If the rules is evaluated truthy, the default rules will both apply, otherwise every property defined on the given object through the prototype chain will be all returned.

> // null is translated to a false
> // all the properties defined on the object are returned
> getPropertyNamesByRules([], null)
> ['length','constructor','toString','toLocaleString','join','pop','push','reverse','shift','unshift','slice','splice','sort','filter','forEach','some','every','map','indexOf','lastIndexOf','reduce','reduceRight','copyWithin','find','findIndex','fill','includes','entries','keys','concat']

You can simply override any of the rules, either the type rule or the property rule, and another one will be set to the corresponding default rule.

If a callback function applies to any of the rules, the callback should return a Boolean (or any value that can be translated to a Boolean) and it will be called upon the given object (this will refer to the given object) with two arguments: the property name and the given object.

getPropertyNamesByRules(t4, {
  ignoredTypes: (type) => type === 'T1'
})

The code below creates a rule to ignore the non-enumerable properties:

getPropertyNamesByRules(t4, {
  ignoredPropreties: (prop, object) => !object.propertyIsEnumerable(prop)
})

If an array is provided, only when any of the array elements (strings, regexps ...) can be evaluated truthy will the rule apply.

getPropertyNamesByRules(t4, {
  ignoredTypes: [/^_+/, 'T1']
})

getAllPropertyNames(object)

This function returns all the property names defined on the given object through the whole prototype chain because no rules apply.

> getAllPropertyNames([])
> ['length','constructor','toString','toLocaleString','join','pop','push','reverse','shift','unshift','slice','splice','sort','filter','forEach','some','every','map','indexOf','lastIndexOf','reduce','reduceRight','copyWithin','find','findIndex','fill','includes','entries','keys','concat']

getNonNativePropertyNames(object)

This function returns the non-native property names (not defined by JavaScript).

> // Array is the native type in JavaScript
> // and so there're no non-native properties found
> getNonNativePropertyNames([])
> []
> 
> const object = {a: 1}
> getNonNativePropertyNames(object)
> ['a']

Usage

ES2015 Module

path: object-property-names/build/index.es.js

import {
	getPropertyNamesByRules,
	getAllPropertyNames,
	getNonNativePropertyNames
} from 'object-property-names'

UMD

path: object-property-names/build/index.umd.js

UMD: CommonJS

var ObjectPropertyNames = require('object-property-names')
var getPropertyNamesByRules = ObjectPropertyNames. getPropertyNamesByRules
var getAllPropertyNames = ObjectPropertyNames. getAllPropertyNames
var getNonNativePropertyNames = ObjectPropertyNames. getNonNativePropertyNames

UMD: AMD

require(['ObjectPropertyNames'], function(ObjectPropertyNames) {
  var getPropertyNamesByRules = ObjectPropertyNames. getPropertyNamesByRules
  var getAllPropertyNames = ObjectPropertyNames. getAllPropertyNames
  var getNonNativePropertyNames = ObjectPropertyNames. getNonNativePropertyNames
})

UMD: IIFE

var getPropertyNamesByRules = window.ObjectPropertyNames. getPropertyNamesByRules
var getAllPropertyNames = window.ObjectPropertyNames. getAllPropertyNames
var getNonNativePropertyNames = window.ObjectPropertyNames. getNonNativePropertyNames

License

MIT. © 2016 Doray Hong

forthebadge