thenBy.js 开源项目教程

thenBy.js 开源项目教程

thenBy.jsthenBy is a javascript micro library that helps sorting arrays on multiple keys项目地址:https://gitcode.com/gh_mirrors/th/thenBy.js

1. 项目的目录结构及介绍

thenBy.js 是一个用于 JavaScript 的库,它扩展了数组的排序功能,允许进行多级排序。项目的目录结构相对简单,主要包含以下几个部分:

thenBy.js/
├── LICENSE
├── README.md
├── package.json
├── src/
│   └── thenBy.js
└── test/
    └── thenBy.test.js
  • LICENSE: 项目的许可证文件。
  • README.md: 项目的介绍文档。
  • package.json: 项目的配置文件,包含依赖、脚本等信息。
  • src/: 源代码目录,包含核心的 thenBy.js 文件。
  • test/: 测试目录,包含测试脚本 thenBy.test.js

2. 项目的启动文件介绍

thenBy.js 的核心功能集中在 src/thenBy.js 文件中。这个文件定义了 firstBythenBy 函数,用于实现多级排序。以下是该文件的主要内容概述:

(function(global) {
    "use strict";

    function extend(obj, src) {
        for (var key in src) {
            if (src.hasOwnProperty(key)) obj[key] = src[key];
        }
        return obj;
    }

    function identity(v) {
        return v;
    }

    function ignoreCase(s) {
        return typeof(s) === 'string' ? s.toLowerCase() : s;
    }

    function Cmp(fn) {
        this.compare = fn || identity;
    }

    Cmp.prototype.thenBy = function(fn, direction) {
        return new ExtendedCmp(this, fn, direction);
    };

    function ExtendedCmp(previous, fn, direction) {
        this.previous = previous;
        this.compare = fn || identity;
        this.direction = direction || 1;
    }

    ExtendedCmp.prototype.thenBy = function(fn, direction) {
        return new ExtendedCmp(this, fn, direction);
    };

    function firstBy(fn, direction) {
        return new Cmp(fn).thenBy(null, direction);
    }

    global.firstBy = firstBy;
})(typeof(window) === 'undefined' ? global : window);
  • firstBy 函数用于初始化排序条件。
  • thenBy 函数用于添加额外的排序条件。

3. 项目的配置文件介绍

项目的配置文件是 package.json,它包含了项目的基本信息、依赖和脚本等。以下是该文件的主要内容概述:

{
  "name": "thenby.js",
  "version": "1.3.0",
  "description": "micro library for sorting arrays using the firstBy().thenBy().thenBy() syntax",
  "main": "src/thenBy.js",
  "scripts": {
    "test": "mocha test/thenBy.test.js"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/Teun/thenBy.js.git"
  },
  "keywords": [
    "sort",
    "array",
    "thenBy",
    "firstBy"
  ],
  "author": "Teun Duynstee",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/Teun/thenBy.js/issues"
  },
  "homepage": "https://github.com/Teun/thenBy.js#readme",
  "devDependencies": {
    "chai": "^4.2.0",
    "mocha": "^6.2.2"
  }
}
  • name: 项目名称。
  • version: 项目版本。
  • description: 项目描述。
  • main: 入口文件。
  • scripts: 脚本命令,如测试命令 npm test
  • repository: 项目仓库信息。
  • keywords: 项目关键词。
  • author: 项目作者。

thenBy.jsthenBy is a javascript micro library that helps sorting arrays on multiple keys项目地址:https://gitcode.com/gh_mirrors/th/thenBy.js

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

毛炎宝Gardener

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值