From 20429750d77a8f16ae4ad5ab02c5ce99a4cdf20a Mon Sep 17 00:00:00 2001 From: afeiship <1290657123@qq.com> Date: Wed, 7 Aug 2024 14:40:45 +0800 Subject: [PATCH] fix: nx.del not exist value should work --- __tests__/base/nx.del.test.js | 7 +++++++ dist/index.js | 3 ++- dist/index.min.js | 2 +- src/core.js | 3 ++- 4 files changed, 12 insertions(+), 3 deletions(-) diff --git a/__tests__/base/nx.del.test.js b/__tests__/base/nx.del.test.js index 60666b1..dfa366b 100644 --- a/__tests__/base/nx.del.test.js +++ b/__tests__/base/nx.del.test.js @@ -1,5 +1,6 @@ const nx = require('../../dist/index'); +// /Users/a7/github/next/__tests__/base/nx.del.test.js describe('nx.get name/path', () => { test('nx.del should worked', () => { var obj = { name: 'afeiship', a: { b: { c: 'c-value' } } }; @@ -10,5 +11,11 @@ describe('nx.get name/path', () => { expect(obj).toEqual({ name: 'afeiship', a: { b: {} } }); }); + test('del path has not exists value should get false', () => { + const obj = { name: 'afei' }; + const res1 = nx.del(obj, 'preferences_tag.US'); + expect(res1).toBe(false); + }); + // https://github.com/sindresorhus/dot-prop/blob/master/test.js }); diff --git a/dist/index.js b/dist/index.js index b7a3fd5..0810204 100755 --- a/dist/index.js +++ b/dist/index.js @@ -235,7 +235,8 @@ else if (freeModule) { var path = paths[i]; if (i === paths.length - 1) { - delete inTarget[path]; + if (inTarget == null) return false; + if (typeof inTarget === 'object') delete inTarget[path]; return true; } inTarget = inTarget[path]; diff --git a/dist/index.min.js b/dist/index.min.js index 340129e..3fa2be2 100755 --- a/dist/index.min.js +++ b/dist/index.min.js @@ -1 +1 @@ -!function(){var a,o,n,e,i,_,s,r,c,f,u,t="object"==typeof global&&global&&global.Object===Object&&global,p="object"==typeof self&&self&&self.Object===Object&&self,t=t||p||Function("return this")(),p="object"==typeof exports&&exports&&!exports.nodeType&&exports,l=p&&"object"==typeof module&&module&&!module.nodeType&&module,h=t.nx=t.nx||{BREAKER:{},NIL:{},VERSION:"1.2.2",DEBUG:!1,GLOBAL:t};function d(t){return t.replace(_,".$1").replace(s,a).replace(r,"")}function y(){}function m(t,e){this.type=t,this.meta=e,this.base=e.extends||h.RootClass,this.$base=this.base.prototype,this.__class_meta__={},this.__class__=null,this.__constructor__=null}"function"==typeof define&&"object"==typeof define.amd&&define.amd?(t.nx=h,define(function(){return h})):l?((l.exports=h).nx=h,p.nx=h):t.nx=h,a=".",o="undefined",n=Array.prototype,e=Object.prototype.toString,i=Object.prototype.hasOwnProperty,_=/\[(\w+)\]/g,s=/[.]+/g,r=/^\.|\.$/g,h.noop=function(){},h.typeof=function(t){return null==t||"object"!=typeof t?null===t?"null":void 0===t?"undefined":typeof t:e.call(t).slice(8,-1).toLowerCase()},h.stubTrue=function(){return!0},h.stubFalse=function(){return!1},h.stubValue=function(t){return t},h.stubPromise=function(t){return Promise.resolve(t)},h.error=function(t){throw new Error(t)},h.try=function(t,e){e=e||h.noop;try{t()}catch(t){e(t)}},h.forEach=function(t,e,_){for(var n=t.length,o=0;o