Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
object-property-names
Advanced tools
Get any property names (enumerable or not) you need from a JavaScript object.
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.
jsnext:main
)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.
getPropertyNamesByRules(object[, rules])
object
whose enumerable and non-enumerable property names to be returnedrules
to create the type/property rulerules = {
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']
path: object-property-names/build/index.es.js
import {
getPropertyNamesByRules,
getAllPropertyNames,
getNonNativePropertyNames
} from 'object-property-names'
path: object-property-names/build/index.umd.js
var ObjectPropertyNames = require('object-property-names')
var getPropertyNamesByRules = ObjectPropertyNames. getPropertyNamesByRules
var getAllPropertyNames = ObjectPropertyNames. getAllPropertyNames
var getNonNativePropertyNames = ObjectPropertyNames. getNonNativePropertyNames
require(['ObjectPropertyNames'], function(ObjectPropertyNames) {
var getPropertyNamesByRules = ObjectPropertyNames. getPropertyNamesByRules
var getAllPropertyNames = ObjectPropertyNames. getAllPropertyNames
var getNonNativePropertyNames = ObjectPropertyNames. getNonNativePropertyNames
})
var getPropertyNamesByRules = window.ObjectPropertyNames. getPropertyNamesByRules
var getAllPropertyNames = window.ObjectPropertyNames. getAllPropertyNames
var getNonNativePropertyNames = window.ObjectPropertyNames. getNonNativePropertyNames
MIT. © 2016 Doray Hong
FAQs
Get any property names you need from a JavaScript object.
We found that object-property-names demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.