-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
31 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.