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

@tarocch1/observer

v0.1.1

Published

An observer for object and array.

Downloads

3

Readme

Observer

一个 Javascript 对象与数组观察器。基于 Proxy API。

npm npm bundle size GitHub Test Workflow

Install

npm install @tarocch1/observer

Usage

Browser

<script src="https://cdn.jsdelivr.net/npm/@tarocch1/observer/dist/index.umd.js"></script>
<script>
  const object = {
    foo: false,
    a: {
      b: [
        {
          c: false,
        },
      ],
    },
  };

  let i = 0;
  const observedObject = Observer.observe(object, function (path, previousValue, value) {
    console.log('Object changed:', ++i);
    console.log('this:', this);
    console.log('path:', path);
    console.log('previousValue:', previousValue);
    console.log('value:', value);
  });

  observedObject.foo = true;
  //=> 'Object changed: 1'
  //=> 'this: {
  //     foo: true,
  //     a: {
  //       b: [
  //         {
  //           c: false
  //         }
  //       ]
  //     }
  //   }'
  //=> 'path: ["foo"]'
  //=> 'previousValue: false'
  //=> 'value: true'
</script>

Node.js

const { observe, observed, unobserve } = require('@tarocch1/observer');

const object = {
  foo: false,
  a: {
    b: [
      {
        c: false,
      },
    ],
  },
};

let i = 0;
const observedObject = observe(object, function (path, previousValue, value) {
  console.log('Object changed:', ++i);
  console.log('this:', this);
  console.log('path:', path);
  console.log('previousValue:', previousValue);
  console.log('value:', value);
});

observedObject.foo = true;
//=> 'Object changed: 1'
//=> 'this: {
//     foo: true,
//     a: {
//       b: [
//         {
//           c: false
//         }
//       ]
//     }
//   }'
//=> 'path: ["foo"]'
//=> 'previousValue: false'
//=> 'value: true'

observedObject.a.b[0].c = true;
//=> 'Object changed: 2'
//=> 'this: {
//     foo: true,
//     a: {
//       b: [
//         {
//           c: true
//         }
//       ]
//     }
//   }'
//=> 'path: ["a", "b", "0", "c"]'
//=> 'previousValue: false'
//=> 'value: true'

// Access the original object
observed(observedObject).foo = false;
// Callback isn't called

// Unobserve
unobserve(observedObject);
observedObject.foo = 'bar';
// Callback isn't called

Edit Observer

API

observe(object, callback, options?)

设置对对象的观察,返回被观察对象。

object

Type: object | array

要观察的对象。

callback(path, previousValue, value)

Type: Function

对象发生变化时的回调。

path

Type: Array<string | symbol>

发生变化的属性的路径。

previousValue

Type: any

变化前的值。

value

Type: any

变化后的值。

options

Type: object

配置项。

isShallow

Type: boolean

Default: false

设置为 true 后,深层变化不会触发回调。

equals

Type: Function

Default: Object.is

用于判断值是否发生变化的函数,接受两个值,如果两个值不等,返回 true

ignoreSymbols

Type: boolean

Default: false

设置为 true 后,属性名为 Symbol 的值发生变化不会触发回调。

ignoreUnderscores

Type: boolean

Default: false

设置为 true 后,属性名以下划线开头的值发生变化不会触发回调。

ignoreKeys

Type: Array<string | symbol>

Default: []

属性名存在于该配置中的值发生变化时不会触发回调。

observed(observedObject)

返回与被观察对象对应的原始对象,对原始对象进行操作不会触发回调。

observedObject

Type: object

observe 函数返回的被观察对象。

unobserve(observedObject)

取消对被观察对象的观察,返回与被观察对象对应的原始对象,取消后对被观察对象进项操作不会触发回调。

observedObject

Type: object

observe 函数返回的被观察对象。

Credit

本项目基于 sindresorhus/on-change,进行了部分精简并修改了几个 bug。