diff --git a/packages/kbn-std/index.ts b/packages/kbn-std/index.ts index 294c168fdd0db..82467cc93d61d 100644 --- a/packages/kbn-std/index.ts +++ b/packages/kbn-std/index.ts @@ -19,7 +19,6 @@ export type { URLMeaningfulParts } from './src/url'; export { isRelativeUrl, modifyUrl, getUrlOrigin } from './src/url'; export { isInternalURL } from './src/is_internal_url'; export { parseNextURL } from './src/parse_next_url'; -export { unset } from './src/unset'; export { getFlattenedObject } from './src/get_flattened_object'; export { ensureNoUnsafeProperties } from './src/ensure_no_unsafe_properties'; export { diff --git a/packages/kbn-std/src/unset.test.ts b/packages/kbn-std/src/unset.test.ts deleted file mode 100644 index 6165423d8539e..0000000000000 --- a/packages/kbn-std/src/unset.test.ts +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the "Elastic License - * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side - * Public License v 1"; you may not use this file except in compliance with, at - * your election, the "Elastic License 2.0", the "GNU Affero General Public - * License v3.0 only", or the "Server Side Public License, v 1". - */ - -import { unset } from './unset'; - -describe('unset', () => { - it('deletes a property from an object', () => { - const obj = { - a: 'a', - b: 'b', - c: 'c', - }; - unset(obj, 'a'); - expect(obj).toEqual({ - b: 'b', - c: 'c', - }); - }); - - it('does nothing if the property is not present', () => { - const obj = { - a: 'a', - b: 'b', - c: 'c', - }; - unset(obj, 'd'); - expect(obj).toEqual({ - a: 'a', - b: 'b', - c: 'c', - }); - }); - - it('handles nested paths', () => { - const obj = { - foo: { - bar: { - one: 'one', - two: 'two', - }, - hello: 'dolly', - }, - some: { - things: 'here', - }, - }; - unset(obj, 'foo.bar.one'); - expect(obj).toEqual({ - foo: { - bar: { - two: 'two', - }, - hello: 'dolly', - }, - some: { - things: 'here', - }, - }); - }); - - it('does nothing if nested paths does not exist', () => { - const obj = { - foo: { - bar: { - one: 'one', - two: 'two', - }, - hello: 'dolly', - }, - some: { - things: 'here', - }, - }; - unset(obj, 'foo.nothere.baz'); - expect(obj).toEqual({ - foo: { - bar: { - one: 'one', - two: 'two', - }, - hello: 'dolly', - }, - some: { - things: 'here', - }, - }); - }); -}); diff --git a/packages/kbn-std/src/unset.ts b/packages/kbn-std/src/unset.ts deleted file mode 100644 index f18fce5175533..0000000000000 --- a/packages/kbn-std/src/unset.ts +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the "Elastic License - * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side - * Public License v 1"; you may not use this file except in compliance with, at - * your election, the "Elastic License 2.0", the "GNU Affero General Public - * License v3.0 only", or the "Server Side Public License, v 1". - */ - -import { get } from './get'; - -/** - * Unset a (potentially nested) key from given object. - * This mutates the original object. - * - * @example - * ``` - * unset(myObj, 'someRootProperty'); - * unset(myObj, 'some.nested.path'); - * ``` - */ -export function unset(obj: OBJ, atPath: string) { - const paths = atPath - .split('.') - .map((s) => s.trim()) - .filter((v) => v !== ''); - if (paths.length === 0) { - return; - } - if (paths.length === 1) { - delete obj[paths[0]]; - return; - } - const property = paths.pop() as string; - const parent = get(obj, paths as any) as any; - if (parent !== undefined) { - delete parent[property]; - } -}