GithubHelp home page GithubHelp logo

inspect-js / is-descriptor Goto Github PK

View Code? Open in Web Editor NEW
12.0 4.0 4.0 100 KB

Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for data descriptors and accessor descriptors.

License: MIT License

JavaScript 100.00%
data-descriptor accessor-descriptor accessor descriptor javascript node nodejs

is-descriptor's Introduction

is-descriptor Version Badge

github actions coverage License Downloads

npm badge

Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for fully completed data descriptors and accessor descriptors.

Usage

const isDescriptor = require('is-descriptor');
const assert = require('assert');

const defaults = { configurable: false, enumerable: false };
const dataDefaults = { ...defaults, writable: false};

assert.ok(isDescriptor({ ...dataDefaults, value: 'foo' }));
assert.ok(isDescriptor({ ...defaults, get() {}, set() {} }));
assert.ok(!isDescriptor({ ...defaults, get: 'foo', set() {} }));

You may also check for a descriptor by passing an object as the first argument and property name (string) as the second argument.

const obj = { foo: 'abc' };

Object.defineProperty(obj, 'bar', { value: 'xyz' });
Reflect.defineProperty(obj, 'baz', { value: 'xyz' });

assert.equal(isDescriptor(obj, 'foo'), true);
assert.equal(isDescriptor(obj, 'bar'), true);
assert.equal(isDescriptor(obj, 'baz'), true);

Examples

value type

Returns false when not an object

assert.equal(isDescriptor('a'), false);
assert.equal(isDescriptor(null), false);
assert.equal(isDescriptor([]), false);

data descriptor

Returns true when the object has valid properties with valid values.

assert.equal(isDescriptor({ ...dataDefaults, value: 'foo' }), true);
assert.equal(isDescriptor({ ...dataDefaults, value() {} }), true);

Returns false when the object has invalid properties

assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', bar: 'baz' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', bar: 'baz' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', enumerable: 'baz' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', configurable: 'baz' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', get() {} }), false);
assert.equal(isDescriptor({ ...dataDefaults, get() {}, value() {} }), false);

false when a value is not the correct type

assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', enumerable: 'foo' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', configurable: 'foo' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', writable: 'foo' }), false);

accessor descriptor

true when the object has valid properties with valid values.

assert.equal(isDescriptor({ ...defaults, get() {}, set() {} }), true);
assert.equal(isDescriptor({ ...defaults, get() {} }), true);
assert.equal(isDescriptor({ ...defaults, set() {} }), true);

false when the object has invalid properties

assert.equal(isDescriptor({ ...defaults, get() {}, set() {}, bar: 'baz' }), false);
assert.equal(isDescriptor({ ...defaults, get() {}, set() {}, enumerable: 'baz' }), false);
assert.equal(isDescriptor({ ...defaults, get() {}, writable: true }), false);
assert.equal(isDescriptor({ ...defaults, get() {}, value: true }), false);

Returns false when an accessor is not a function

assert.equal(isDescriptor({ ...defaults, get() {}, set: 'baz' }), false);
assert.equal(isDescriptor({ ...defaults, get: 'foo', set() {} }), false);
assert.equal(isDescriptor({ ...defaults, get: 'foo', bar: 'baz' }), false);
assert.equal(isDescriptor({ ...defaults, get: 'foo', set: 'baz' }), false);

Returns false when a value is not the correct type

assert.equal(isDescriptor({ ...defaults, get() {}, set() {}, enumerable: 'foo' }), false);
assert.equal(isDescriptor({ ...defaults, set() {}, configurable: 'foo' }), false);
assert.equal(isDescriptor({ ...defaults, get() {}, configurable: 'foo' }), false);

Related projects

You might also be interested in these projects:

  • is-accessor-descriptor: Returns true if a value has the characteristics of a valid JavaScript accessor descriptor.
  • is-data-descriptor: Returns true if a value has the characteristics of a valid JavaScript data descriptor.
  • is-object: Returns true if the value is an object and not an array or null.

Tests

Simply clone the repo, npm install, and run npm test

is-descriptor's People

Contributors

doowb avatar jonschlinkert avatar ljharb avatar realityking avatar wtgtybhertgeghgtwtg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

is-descriptor's Issues

Some issues with the checkings

Hello, I may have found a problem with the is-data-descriptor and is-accessor-descriptor checkings.

A data property descriptor is one that includes any fields named either [[Value]] or [[Writable]]. An accessor property descriptor is one that includes any fields named either [[Get]] or [[Set]]. Any property descriptor may have fields named [[Enumerable]] and [[Configurable]]. A Property Descriptor value may not be both a data property descriptor and an accessor property descriptor; however, it may be neither.
-- The Property Descriptor and Property Identifier Specification Types

This one works fine:

// object: { test: 'value' }
var descriptor =  {
    value: 'value',
    writable: true,
    enumerable: true,
    configurable: true
};

isDataDescriptor(descriptor ); // true - right
isAccessor(descriptor ); // false - right

But, if we do this:

/*
object: {
    get test() {
        // Return something
    },

    set test(value) {
        // Do something
    }
}
*/
var descriptor = {
    get: [Function: test],
    set: [Function: test],
    enumerable: true,
    configurable: true
};

isDataDescriptor(descriptor); // true - wrong
isAccessor(descriptor); // true - right

Which is wrong, because it doesn't have neither the [[value]] or [[writable]] attributes.

This one { get: [Function: test], set: undefined, enumerable: true, configurable: true } is not even considered a descriptor either. But, as an accessor descriptor it can have either [[get]] or [[set]] and should return false only if doesn't have any, as you can read here:

When the abstract operation IsAccessorDescriptor is called with property descriptor Desc, the following steps are taken:

  1. If Desc is undefined, then return false.
  2. If both Desc.[[Get]] and Desc.[[Set]] are absent, then return false.
  3. Return true.

-- IsAccessorDescriptor ( Desc )

Issue importing kind-of dependancy

Hey,

I am actually trying to use karma-server-side module with karma, and i am running into some issue with the 'is-descriptor' dependency importing kind-of. I am not sure if this is a bug or an issue with my configuration, but any help would be greatly appreciated.

here is the failure output.
Cannot find module 'kind-of'.
Simple.test.js:8902:88
[email protected]:8902:90
[email protected]:8899:51
[email protected]:8670:32
[email protected]:8588:13
[email protected]:8559:20
[email protected]:8524:14
[email protected]:8466:18
[email protected]:8390:26
Simple.test.js:56:20

here is a reduced version of the setup i am using.

tools/
webpack.shared.config.js
src/
Simple.test.js
package.json
karma.conf.json

package.json

{
  "description": "",
  "scripts": {
    "test": "karma start karma.conf.js --single-run",
    "test:debug": "karma start karma.conf.js --browsers=Chrome --single-run=false"
  },
  "keywords": [],
  "devDependencies": {
    "autoprefixer": "^6.3.7",
    "babel-core": "^6.10.4",
    "babel-loader": "^6.2.4",
    "babel-plugin-add-module-exports": "^0.2.1",
    "babel-plugin-transform-runtime": "^6.9.0",
    "babel-polyfill": "^6.16.0",
    "babel-preset-es2015": "^6.16.0",
    "babel-preset-react": "^6.16.0",
    "chai": "^3.5.0",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-mocha": "^1.1.1",
    "karma-mocha-reporter": "^2.2.0",
    "karma-phantomjs-launcher": "^1.0.1",
    "karma-server-side": "^1.5.0",
    "karma-webpack": "^1.7.0",
    "mocha": "^3.1.2",
    "postcss-loader": "^0.11.1",
    "precss": "^1.4.0",
    "webpack": "^1.13.1"
  },
  "dependencies": {
    "babel-runtime": "^6.11.6"
  }
}

karma.conf.json

// Karma configuration
const path = require('path');
const webpackConfig = require('./tools/webpack.shared.config');
const extend = require('extend');

module.exports = (config) => {
    config.set({
        basePath: 'src',
        frameworks: ['mocha', 'server-side'],
        files: [
            '**/Simple.test.js',
        ],
        exclude: [],
        preprocessors: {
            '**/*.test.js': ['webpack'],
        },
        reporters: ['mocha'],
        port: 9876,
        colors: true,
        logLevel: config.LOG_INFO,
        autoWatch: true,
        browsers: ['PhantomJS'],
        singleRun: false,
        concurrency: 1,
        webpack: webpackConfig,
    });
};

webpack.shared.config.js

const path = require('path');
const webpack = require('webpack');
const plugins = [];

if (process.env.NODE_ENV) {
    plugins.push(new webpack.optimize.UglifyJsPlugin());
}

module.exports = {
    resolve: {
        extensions: ['', '.js', '.jsx', '.json'],
    },
    module: {
        loaders: [{
            test: /\.js?$/,
            exclude: /node_modules/,
            loaders: [
                'babel-loader?cacheDirectory&presets[]=es2015&plugins[]=transform-runtime',
            ],
        }],
    },
    plugins,
};

Simple.test.js

/* eslint-env mocha */
import { assert } from 'chai';

const server = require('karma-server-side');

describe('Test send request', () => {
    it('karma-server-side test', (done) => {
        const server = require('karma-server-side');
        server.run(() => {
            return 'success';
        }).then((result) => {
            assert.equal(result, 'success');
            done();
        });
    });
});

the workflow i am running is just to run the following commands
'npm install'
'npm test'

additionally i do see these warnings:
WARNING in .//is-descriptor/utils.js
Critical dependencies:
7:34-41 require function is used in a way in which dependencies cannot be statically extracted
@ ./
/is-descriptor/utils.js 7:34-41

WARNING in ./~/is-descriptor/package.json
Module parse failed: /Users/mhenley/git/scrd/milestest/node_modules/is-descriptor/package.json Unexpected token (2:9)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (2:9)

Any ideas? could this be a bug? or am i just doing something wrong?

Thanks!

0.1.6 tag is missing on github

Hi,

in our company we are using 0.1.6 and I can see that version on npmjs. However, for some reason the 0.1.6 to has not been set in git. In fact when I look at the commits for 1.0.0 I can see that the version jumped directly from 0.1.5 to 1.0.0. How come there appears to be some mismatch? This is causing an issue in our licensing department. Is it possible to add that tag retrospectively?

Any help on this would be greatly appreciated.

Thanks!
Michael

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.