JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q31015F
  • License ISC

自家前端交流在项目开发中构建的JS工具库,内含项目中最常用的JS工具函数

Package Exports

  • own-js
  • own-js/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (own-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

简介

自家前端交流构建的JS工具库,内含丰富的常用的JS函数

安装

npm i own-js@latest

使用

import {combineDistinct} from 'own-js'
const res = combineDistinct([ 1, 2, 3 ], [ 1, 2, 4 ], [ 1, 2, 5 ])
// [ 1, 2, 3, 4, 5 ]

Api

数组

combineDistinct

数组去重

  • 参数:若干个数组
  • 返回值:去重后的数组

示例

const res = combineDistinct([ 1, 2, 3 ], [ 1, 2, 4 ], [ 1, 2, 5 ])
// [ 1, 2, 3, 4, 5 ]

arrayDistinct

去重数组-对象数组,根据对象的某个字段进行去重

  • source 原始数组
  • property 根据该属性去重

示例

// 用法如下
const datas = [
  {
    name: "liu",
    type: "1"
  },
  {
    name: "wang",
    type: "2"
  },
  {
    name: "wang",
    type: "1"
  }
]
console.log(arrayDistinct(datas,'name'))

sortBy

排序数组-对数组某个对象按照某个字段来排序

  • property 排序字段
  • isAsc 排序方式,默认升序

示例

let arr1 = [
  { count: 1, name: '小米' },
  { count: 3, name: '华为' },
  { count: 2, name: '苹果' }
]
// sort() 方法用原地算法对数组的元素进行排序,并返回数组。
const res = arr1.sort(sortBy('count', false))
// 结果
// [
//     {
//         "count": 3,
//         "name": "华为"
//     },
//     {
//         "count": 2,
//         "name": "苹果"
//     },
//     {
//         "count": 1,
//         "name": "小米"
//     }
// ]

groupBy

数组分组-对数组对象进行分组

  • source 原始数组
  • property 分组属性

示例

let people = [
  { name: 'Alice', age: 21 },
  { name: 'Max', age: 20 },
  { name: 'Jane', age: 20 }
]
let groupedPeople = groupBy(people, 'age')
// 结果
// {
//   20: [
//     { name: 'Max', age: 20 },
//     { name: 'Jane', age: 20 }
//   ],
//   21: [{ name: 'Alice', age: 21 }]
// }

filterBy

过滤数组(支持递归过滤)

  • source 原始数组
  • filters 过滤条件(数组)
  • property 根据该属性过滤
  • children 子节点属性名,无子节点不用传或者传空

示例

const menus1 = [
  {
    title: '添加1级',
    permission: 'add',
    children: [
      {
        title: '添加2级',
        permission: 'add1-2',
        children: [
          {
            title: '添加3级',
            permission: 'add1-3'
          },
          {
            title: '添加2-3',
            permission: 'add2-3'
          }
        ]
      },
      {
        title: '添加2-2',
        permission: 'add2-2'
      }
    ]
  }, {
    title: '修改',
    permission: 'edit'
  }, {
    title: '删除',
    permission: 'delete'
  }, {
    title: '查询',
    permission: 'query'
  }
]
const myPermissions1 = [ 'add', 'query', 'add1-2', 'add1-3' ]
const p1 = filterBy(menus1, myPermissions1, 'permission', 'children')
// 结果  
//   [
//     {
//       "title": "添加1级",
//       "permission": "add",
//       "children": [
//         {
//           "title": "添加2级",
//           "permission": "add1-2",
//           "children": [
//             {
//               "title": "添加3级",
//               "permission": "add1-3"
//             }
//           ]
//         }
//       ]
//     },
//     {
//       "title": "查询",
//       "permission": "query"
//     }
//   ]

校验

validateIdCard

身份证号校验

  • idcard 身份证号
  • 验证成功返回空字符串,验证失败返回错误消息

    示例

validateIdCard('110110198801011234')

事件