Skip to content

Commit

Permalink
新增文件: 增加yd_object_repair函数
Browse files Browse the repository at this point in the history
  • Loading branch information
chenbimo committed Aug 5, 2024
1 parent b4a4c72 commit c241f53
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
22 changes: 22 additions & 0 deletions lib/object/repair.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 修复对象值
* @param {object} obj 对象数据
* @param {Array} values 要修复的值
* @param {any} replace 要替换的值
* @returns {object} 返回修复后的对象
* @example yd_object_repair({a:null,b:undefined,c:1}) // {a:'',b:'',c:1}
*/
export default (obj, values = [null, undefined], replace = '') => {
const newObj = {};
for (let key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const value = obj[key];
if (values.includes(value)) {
newObj[key] = replace;
} else {
newObj[key] = value;
}
}
}
return newObj;
};
9 changes: 9 additions & 0 deletions lib/object/repair.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, it, expect } from 'vitest';
import yd_object_repair from './repair.js';

describe('yd_object_repair', () => {
it('should return repaired object', () => {
expect(yd_object_repair({ a: 1, b: undefined, c: null, d: 'a' })).toStrictEqual({ a: 1, b: '', c: '', d: 'a' });
expect(yd_object_repair({ a: 1, b: undefined, c: null, d: 'a' }, [undefined])).toStrictEqual({ a: 1, b: '', c: null, d: 'a' });
});
});
23 changes: 0 additions & 23 deletions lib/object/repairEmpty.js

This file was deleted.

0 comments on commit c241f53

Please sign in to comment.