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

@cherry-tomato/core

v3.1.2

Published

[![npm version](https://img.shields.io/npm/v/@cherry-tomato/core.svg?maxAge=3600)](https://www.npmjs.org/package/@cherry-tomato/core)

Downloads

86

Readme

cherry-tomato

npm version

介绍

cherry-tomato 基于面向对象的设计理念,主要专注于在前端领域里,抽象数据模型和业务逻辑。让逻辑和视图解耦,提高项目代码的可复用性。

cherry-tomato 基于 typescript 开发,是纯粹的 js 代码。

安装

npm install --save @cherry-tomato/core

目录

核心API

Model模型

基础模型类。最基础也是重要的,一般需要配合 attribute 装饰器使用

Usage用法

import {
  Model,
  attribute
} from '@cherry-tomato/core';

class CustomModel extends Model {
  @attribute()
  accessor text = 'a'; // 设置默认值为a
}

const custom = new CustomModel();
custom.text === 'a';
Arguments 实例化时的可选参数
  • [attributes] 初始化属性
const custom = new CustomModel({ text: 'b' });
custom.text === 'b';
Method 内置函数(同时包含 EventEmitter 的内容)
  • get(key) 获取属性,设置过的属性可以被通过attribute装饰过的属性直接获取
  • set(key, value) 设置属性
  • reset(value) 重设所有属性
  • remove(key) 删除对应属性

使用例子如下

custom.set('text', 'c');
custom.text === 'c';
custom.get('text') === 'c';
Static Attributes 静态函数
  • [initialAttributes] {Function} - 将会把返回值作为初始化的属性。现在更推荐使用class fields的方式设置默认值
  • key - key生成函数,默认无需设置,会自动生成

Hooks 生命周期

model设置了2个生命周期,同时会抛出同名的Event

  1. modelWillUpdate {Function([prevAttribute, nextAttribute])} - 调用了set, 但还没有执行set 操作时,此时 this.get(attributeName) === prevAttributes.get(attributeName)
  2. modelDidUpdate {Function([prevAttribute, nextAttribute])} - 调用了set, set执行成功, 此时 this.get(attributeName) === nextAttributes.get(attributeName)

Collection集合

继承自Model, 扩充对数组类型的支持。会更具static Model的静态属性自动转换children对对应的对象实例

import {
  Collection
} from '@cherry-tomato/core';
class CustomCollection extends Collection<CustomModel> {
  static Model = CustomModel;

  // hook: before update children (includes remove, add)
  collectionWillUpdateChildren () {}
  // hook: after update children (includes remove, add)
  collectionDidUpdateChildren () {}
}

const customCollection = new CustomCollection();
customCollection.resetChildren(
  [
    {
      text: 'a'
    },
    {
      text: 'b'
    }
  ]
);

customCollection.children[0] instanceof CustomModel;
customCollection.children[0].text === 'a';
customCollection.children[1].text === 'b';
Arguments 实例化时的可选参数
  • [initialAttributes]ModelinitialAttributes
Static Attributes 静态函数
  • Model - 用于自动生成子元素的构造函数
  • 其他继承于 Model
Method 内置函数(同时包含 Model 的内容)
  • Model 所有函数
  • 可以使用Array的各种方法 已支持forEach, map, reduce, reduceRight, slice, filter, find, findIndex, some, every, includes, indexOf
      customCollection.forEach((custom) => {
        console.log(custom.text)
      })
      // 'a'
      // 'b'
    特别注意:filter、slice、concat 返回的是一个collection实力
  • addChild - 添加一个子元素到最后,并添加监听,会自动使用设置的 Static Model 去创建
    • @params item {Object} - 子元素属性内容
  • removeChild - 移除一个子元素,并取消监听,会自动使用设置的 Static Model 去创建
    • @params item {Model} - 子元素实例
  • resetChildren - 重设所有子元素,并添加监听,会自动使用设置的 Static Model 去创建
    • @params items {Array} - 子元素数据数组
  • merge 效果同 concat,同时会修改自身

Hooks 生命周期

  • Model的所有hooks
  • collectionWillUpdateChildren {Function([prevChildren: Array, nextChildren: Array])} - 添加/删除子元素之前后触发
  • collectionDidUpdateChildren {Function([prevChildren: Array, nextChildren: Array])} - 添加/删除子元素后触发
  • collectionChildDidUpdate - 任一子元素发生modelDidUpdate, collectionDidUpdateChildren, collectionDidUpdateChildren事件后触发

attribute属性装饰器

参考Model就可以了

connect关联模型装饰器

高级用法,关联不同的实例

使用方法

import {
  Model,
  connect,
  attribute
} from '@cherry-tomato/core';

class ModelA extends Model {
  @attribute()
  accessor text = 'a';
}

class ModelB extends Model {
  @connect()
  a = new ModelA();
}

const b = new ModelB({
  a: {
    text: 'aa'
  }
});

b.a instanceof ModelA;
b.a.text === 'aa';

自定义event事件

待补充