Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@antv/util
Advanced tools
@antv/util is a utility library that provides a collection of functions for common programming tasks. It is part of the AntV ecosystem, which is a suite of data visualization tools. The library includes functions for data manipulation, type checking, deep cloning, and more.
Type Checking
The @antv/util package provides various type-checking functions to determine the type of a given value. In this example, `isString` is used to check if a value is a string.
const isString = require('@antv/util/lib/type/isString');
console.log(isString('Hello World')); // true
console.log(isString(123)); // false
Deep Clone
The `deepClone` function creates a deep copy of an object, ensuring that nested objects are also cloned. This is useful for creating independent copies of complex data structures.
const deepClone = require('@antv/util/lib/deepClone');
const obj = { a: 1, b: { c: 2 } };
const clonedObj = deepClone(obj);
console.log(clonedObj); // { a: 1, b: { c: 2 } }
console.log(clonedObj === obj); // false
Data Manipulation
The `groupBy` function allows you to group an array of objects by a specified key. This is useful for organizing data into categories or groups.
const groupBy = require('@antv/util/lib/array/groupBy');
const data = [
{ category: 'fruit', name: 'apple' },
{ category: 'fruit', name: 'banana' },
{ category: 'vegetable', name: 'carrot' }
];
const groupedData = groupBy(data, 'category');
console.log(groupedData); // { fruit: [{ category: 'fruit', name: 'apple' }, { category: 'fruit', name: 'banana' }], vegetable: [{ category: 'vegetable', name: 'carrot' }] }
Lodash is a popular utility library that provides a wide range of functions for data manipulation, type checking, and more. It is widely used and has a large community. Compared to @antv/util, Lodash offers a more extensive set of utilities and is more commonly used in the JavaScript ecosystem.
Underscore is another utility library that offers a variety of functions for common programming tasks. It is similar to Lodash but has a smaller footprint. While @antv/util is part of the AntV ecosystem, Underscore is a standalone library that focuses solely on utility functions.
Ramda is a functional programming library for JavaScript that emphasizes immutability and side-effect-free functions. It provides a different approach compared to @antv/util, focusing on functional programming paradigms. Ramda is ideal for developers who prefer a functional style of coding.
为
antv
开发的轻量级工具方法库。
tnpm i --save @antv/util
// 所有的 api 是都这么引入,名字不同而已
import { each, get } from '@antv/util';
each(arr, (item, idx) => {
});
const x = get(obj, 'a.b', '');
目前使用到的、且推荐使用的 API 文档,不在文档内的不建议使用。
后续方法添加到文档需要经过审核:
推荐使用的 API 文档如下:
array
event
object
string
type
function
format
math
animate
other
TODO 完善上述各个方法的使用实例。
import { contains } from '@antv/util';
const has = contains([1, 2, 3], 1);
import { startsWith } from '@antv/util';
startsWith([1, 2, 3], 1);
// true
startsWith('abc', 'b');
// false
import { endsWith } from '@antv/util';
endsWith([1, 2, 3], 1);
// false
endsWith('abc', 'c');
// true
import { groupBy } from '@antv/util';
groupBy([6.1, 4.2, 6.3], Math.floor);
// => { '4': [4.2], '6': [6.1, 6.3] }
// 根据元素键值来分组
groupBy([ { user: 'lily' }, { user: 'lucy' } ], 'user');
import { groupBy } from '@antv/util';
group([6.1, 4.2, 6.3], Math.floor);
// => [ [4.2], [6.1, 6.3] ]
group([ { user: 'lily' }, { user: 'lucy' } ], 'user');
// [ [{ user: 'lily' }], [{ user: 'lucy' }] ]
缓存方法的执行结构,一般用于耗时的计算方法。
import { memoize } from '@antv/util';
function max(...args) {
return Math.max(...args);
}
// 第二个参数,是将变量变成 key,如果没有,则直接取第一个参数作为 key
const mmax = memoize(max, (...args) => args.join('-'));
mmax(1, 2, 3, 4, 5);
判断是否是有限数
import { isFinite } from '@antv/util';
isFinite(3);
// => true
isFinite('3');
// => false
按照 path 给 obj 赋值。方法是 mutable 的。
import { set } from '@antv/util';
set({ a: { b: { c: 1 } } }, 'a.b', 100);
// return the original object.
FAQs
> AntV 底层依赖的工具库,不建议在自己业务中使用。
The npm package @antv/util receives a total of 249,934 weekly downloads. As such, @antv/util popularity was classified as popular.
We found that @antv/util demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 69 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.