Skip to content

Commit

Permalink
0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yanhaijing committed Sep 28, 2023
1 parent 13168db commit 7a94ee8
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 87 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGE LOG

## 0.2.0 / 2023-9-23

- paths 支持 type 类型

## 0.1.1 / 2023-9-23

- fix miss @babel/runtime-corejs3 in dependencies
Expand Down
157 changes: 157 additions & 0 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# [anypath](https://github.com/jsmini/anypath)

[![](https://img.shields.io/badge/Powered%20by-jslib%20base-brightgreen.svg)](https://github.com/yanhaijing/jslib-base)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jsmini/anypath/blob/master/LICENSE)
[![CI](https://github.com/jsmini/anypath/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/jsmini/anypath/actions/workflows/ci.yml)
[![npm](https://img.shields.io/badge/npm-0.2.0-orange.svg)](https://www.npmjs.com/package/@jsmini/anypath)
[![NPM downloads](http://img.shields.io/npm/dm/@jsmini/anypath.svg?style=flat-square)](http://www.npmtrends.com/@jsmini/anypath)
[![Percentage of issues still open](http://isitmaintained.com/badge/open/jsmini/anypath.svg)](http://isitmaintained.com/project/jsmini/anypath 'Percentage of issues still open')

最好用的深层取值赋值库,支持 Object、Array 和 Map。

[Engilsh](./README.md) | 简体中文

虽然深层取值我们可以使用ES新的`?`语法,避免报错,如下所示:

```js
const a = { b: { c: 1 } };
console.log(a?.b?.c); // 1
```
但对于深层赋值操作,我们就无能为力了,如下所示:
```js
const a = {};
a?.b?.c = 1; // 报错
```
但是使用本库,我们可以轻松实现深层取值和赋值,如下所示:
```js
import { get, set } from '@jsmini/anypath';

const a = {};
set(a, 'b.c', 1);
console.log(get(a, 'b.c')); // 1
```
支持 Object、Array 和 Map,如下所示:
```js
import { set } from '@jsmini/anypath';

const a = {};
set(a, 'b:map.c[].0', 1);
```
## 兼容性
单元测试保证支持如下环境:
| IE | CH | FF | SF | OP | IOS | 安卓 | Node |
| --- | --- | --- | --- | --- | --- | ---- | ----- |
| 6+ | 23+ | 4+ | 6+ | 10+ | 5+ | 2.3+ | 0.10+ |
## 目录介绍
```
.
├── demo 使用demo
├── dist 编译产出代码
├── doc 项目文档
├── src 源代码目录
├── test 单元测试
├── CHANGELOG.md 变更日志
└── TODO.md 计划功能
```
## 如何使用
通过npm下载安装代码
```bash
$ npm install --save @jsmini/anypath
```
如果你是node环境
```js
var name = require('@jsmini/anypath').name;
```
如果你是webpack等环境
```js
import { name } from '@jsmini/anypath';
```
如果你是浏览器环境
```html
<script src="node_modules/@jsmini/anypath/dist/index.aio.js"></script>

<script>
var name = jsmini_anypath.name;
</script>
```
## 文档
- [API](https://github.com/jsmini/anypath/blob/master/doc/api.md)
## 贡献指南 ![PRs welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)
首次运行需要先安装依赖
```bash
$ npm install
```
一键打包生成生产代码
```bash
$ npm run build
```
运行单元测试,浏览器环境需要手动测试,位于`test/browser`
```bash
$ npm test
```
修改package.json中的版本号,修改README.md中的版本号,修改CHANGELOG.md,然后发布新版
```bash
$ npm run release
```
将新版本发布到npm
```bash
$ npm publish
```
重命名项目名称,首次初始化项目时需要修改名字,或者后面项目要改名时使用,需要修改`rename.js`中的`fromName``toName`,会自动重命名下面文件中的名字
- README.md 中的信息
- package.json 中的信息
- config/rollup.js 中的信息
- test/browser/index.html 中的仓库名称
```bash
$ npm run rename # 重命名命令
```
## 贡献者列表
[contributors](https://github.com/jsmini/anypath/graphs/contributors)
## 更新日志
[CHANGELOG.md](https://github.com/jsmini/anypath/blob/master/CHANGELOG.md)
## 计划列表
[TODO.md](https://github.com/jsmini/anypath/blob/master/TODO.md)
## 谁在使用
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
[![](https://img.shields.io/badge/Powered%20by-jslib%20base-brightgreen.svg)](https://github.com/yanhaijing/jslib-base)
[![license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jsmini/anypath/blob/master/LICENSE)
[![CI](https://github.com/jsmini/anypath/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/jsmini/anypath/actions/workflows/ci.yml)
[![npm](https://img.shields.io/badge/npm-0.1.1-orange.svg)](https://www.npmjs.com/package/@jsmini/anypath)
[![npm](https://img.shields.io/badge/npm-0.2.0-orange.svg)](https://www.npmjs.com/package/@jsmini/anypath)
[![NPM downloads](http://img.shields.io/npm/dm/@jsmini/anypath.svg?style=flat-square)](http://www.npmtrends.com/@jsmini/anypath)
[![Percentage of issues still open](http://isitmaintained.com/badge/open/jsmini/anypath.svg)](http://isitmaintained.com/project/jsmini/anypath 'Percentage of issues still open')

The best library for deep value assignment.
The best deep value assignment library, supporting Object, Array, and Map.

Engilsh | [简体中文](./README-zh_CN.md)

While we can use the new ? syntax of ES for getting deep values to avoid errors, as shown below:

Expand All @@ -33,6 +35,15 @@ set(a, 'b.c', 1);
console.log(get(a, 'b.c')); // 1
```
supporting Object, Array, and Map,as shown below:
```js
import { set } from '@jsmini/anypath';

const a = {};
set(a, 'b:map.c[].0', 1);
```
## Environment Support
unit test ensure it supports the following environments.
Expand Down Expand Up @@ -117,7 +128,7 @@ $ npm run release
publish the new package to npm
```bash
$ npm publish --access=public
$ npm publish
```
rename project. you need to edit project name when initialize project or anytime you want to rename the project . you need to rename `formName` and `toname` in file `rename.js`,which will automatically rename project name in the following files
Expand Down
7 changes: 4 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# TODO List

- [ ] 支持数组key a.0.c
- [ ] 支持数组key a[0].c
- [ ] paths 支持 type 类型
- [x] `paths` 支持 `type` 类型
- [ ] 支持 'a.0.c' 语法,此时`a`自动识别为数组
- [ ] 支持 'a[0].c' 语法,此时`a`自动识别为数组
- [ ] 支持 'a[0][1].c' 语法,此时`a`,`a[0]`自动识别为数组
115 changes: 60 additions & 55 deletions doc/api.md
Original file line number Diff line number Diff line change
@@ -1,107 +1,112 @@
# Documentation
# 文档

This is a JavaScript library for working with nested data, including functionalities such as get, set, getAnypath, and setAnypath.
这是一个用于操作嵌套数据的js库,包括get、set、getAnypath和setAnypath等功能。

## get Function
## get函数

The get function is a simplified version of getAnypath. It uses a string key separated by "." to extract nested values.
get函数是getAnypath的简化形式,它使用由"."分隔的字符串键提取嵌套值。

Detailed Description: This function accepts a dot notation string and retrieves the nested properties accordingly.
参数和返回遵循以下规则:

The parameters and return follow these rules:
- param { any } obj 从中获取嵌套值的对象/数组/映射。
- param { string } keys 点符号表示的键字符串,用来获取嵌套值。
- return { any } 从嵌套路径中得到的值,否则为undefined。

- param {any} obj Object/array/map from which to get the nested value.
- param {string} keys Dot notation string representing the key to access the nested value.
- return {any} The value obtained from the nested path, or undefined if not found.

Example:
示例:

```ts
const obj = {
a: {
b: {
c: 2,
},
b: new Map([['c', [1, 2. 3]]]),
},
};
console.log(get(obj, 'a.b.c')); // 2
console.log(get(obj, 'a.b.c.0')); // 1
console.log(get(obj, 'a.b.c.1')); // 2
console.log(get(obj, 'a.b.c.2')); // 3
```

## set Function

The set function is a simplified version of setAnypath. It sets values within nested object/array/map.
## set函数

Detailed Description: This function accepts a dot notation string and sets the value to the nested property accordingly.
set函数是setAnypath的简化形式,它在嵌套对象/数组/Map中设置值。

The parameters and return follow these rules:
参数和返回遵循以下规则:

- param {any} obj Object/array/map where the nested value will be set.
- param {string} keys Dot notation string representing the key to set the nested value.
- param {any} value The value to set.
- return {boolean} Returns true if the value was successfully set, false otherwise.
- param { any } obj 要设置嵌套值的对象/数组/映射。
- param { string } keys 点符号表示的键字符串,用来设置嵌套值。
- param { any } value 要设置的值。
- return { boolean } 如果值成功设置,返回true,否则返回false。

Example:
示例:

```ts
const obj = {};
console.log(set(obj, 'a.b.c', 2));
// Returns true, now obj equals to { a: { b: { c: 2}}}
console.log(set(obj, 'a.b:map.c[].0', 2));
// 返回结果为true,现在的obj对象将是:
// {
// a: {
// b: new Map([['c', [2]]]),
// },
// };
```

Note: If any part of the path doesn't exist, it will be created and defaulted to an empty object.
注意:如果路径的任何部分不存在,它将被创建并默认设置为空对象,可以通过`:`语法和`[]`指定数据类型。

## getAnypath Function
## getAnypath函数

The getAnypath function is used to get nested values from an object/array/map.
getAnypath函数用于从对象/数组/映射中获取嵌套值。

Detailed Description: It can retrieve values from directly nested properties (like a.b.c) or from deeper array/map levels.
函数的参数和返回遵循以下规则:

The function parameters and return follow these rules:
- param { any } obj 用于访问属性的对象/数组/映射。
- param { AnyPath[] } paths 一个路径数组,用于到达所需的值。
- return { any } 如果路径存在,从路径中找到的值,否则为undefined。

- param {any} obj Object/array/map used to access properties.
- param {AnyPath[]} paths An array of paths to reach the desired value.
- return {any} The value found in the path, if it exists, otherwise it returns undefined.

Example:
示例:

```ts
const obj = {
a: {
b: {
c: 2,
},
b: new Map([['c', [1, 2. 3]]]),
},
};
console.log(getAnypath(obj, [{ key: 'a' }, { key: 'b' }, { key: 'c' }])); // 2
console.log(getAnypath(obj, [{ key: 'a' }, { key: 'b' }, { key: 'c' }, { key: 1 }])); // 2
```

Note: Empty or incorrect paths will result in undefined.
注意:空路径或错误路径将导致结果为undefined。

## setAnypath Function
## setAnypath函数

The setAnypath function sets a value in a complex nested object/array/map.
setAnypath函数将值设置在复杂的嵌套对象/数组/映射中。

Detailed Description: This function can set values to deeply nested properties, even if parts of the path don't exist. It will create missing parts of the path with default values.
当路径不存在,它会用默认值创建路径的缺失部分。

The parameters and return follow these rules:
参数和返回遵循以下规则:

- param {any} obj Object/array/map where the value will be set.
- param {AnyPath[]} paths An array of paths to determine where to set the value.
- param {any} value The value to set.
- return {boolean} Returns true if the value was successfully set, false otherwise.
- param { any } obj 要设置值的对象/数组/映射。
- param { AnyPath[] } paths 一个路径数组,用来确定要设置值的位置。
- param { any } value 要设置的值。
- return { boolean } 如果值成功设置,返回true,否则返回false。

Example:
示例:

```ts
const obj = {};
console.log(
setAnypath(
obj,
[{ key: 'a' }, { key: 'b', defaultValue: () => ({}) }, { key: 'c' }],
[
{ key: 'a' }, // 当type和defaultValue都未指定时,默认为object
{ key: 'b', type: 'map' }, // 指定type
{ key: 'c', defaultValue: () => [1] }, // 指定默认值
{ key: '1' },
],
2,
),
);
// Returns true, now obj equals to { a: { b: { c: 2}}}
// 返回结果为true,现在的obj对象将是:
// {
// a: {
// b: new Map([['c', [1, 2]]]),
// },
// };
```

Note: The setAnypath function cannot work in cases where the path is invalid or when trying to set a value to a non-object value.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@jsmini/anypath",
"version": "0.1.1",
"description": "The best library for deep value assignment",
"version": "0.2.0",
"description": "The best deep value assignment library, supporting Object, Array, and Map.",
"sideEffects": false,
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand Down
Loading

0 comments on commit 7a94ee8

Please sign in to comment.