1.1.1 • Published 5 years ago

covert-alias v1.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

covert-alias

npm version Build Status codecov

通过script标签使用

<script src="https://unpkg.com/covert-alias/dist/index.js"></script> 

npm使用

npm install covert-alias

什么是covert-alias

covert-alias用来简单的进一些数据的转换应用格式。通常前后端联调的时候。后台不会把数据与字段名对应。往往通常需要进行转换。

举个例子:

在vue中有一个select框中需要写入的数据通过封装为传入的value展示的为value字段, label展示的为label字段, 而后台返回的则的是以下情况

使用方式

数组

const { cArrayPur } from 'covert-alias'
const array = ['kitty', true]
const value = cArrayPur(array, ['label', 'value'])
console.log(value)
// [ { labe: 'kitty', value: 'kitty' }, { label: true, value: true }]

对象数组

const { cArray } from 'covert-alias'
const array = [
  { name: 'kitty', id: 1, age: 1 },
  { name: 'peter', id: 2, age: 2 },
  { name: 'ben', id: 3, age: 3 }
]
const value = cArray(array, { label: 'name', value: 'id' }, 'all') 

// 如果传入all则把全本的数据一并合并返回
//const array = [
//  { name: 'kitty', id: 1, age: 1, label: 'kitty', value: 1 },
//  { name: 'peter', id: 2, age: 2, label: 'peter', value: 2 },
//  { name: 'ben', id: 3, age: 3, label: 'ben', value: 3 }
//]

const value = cArray(array, { label: 'name', value: 'id' }) 

// 如果第三个参数不传任何东西则只会返回替换字段后的结果
//const array = [
//  { label: 'kitty', value: 1 },
//  { label: 'peter', value: 2 },
//  { label: 'ben', value: 3 }
//]
1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago