Deep-omit dot paths from an object with * wildcard segments
Before an API response is written to the client-side analytics log, a sanitizer strips the fields that must never leave the server: passwords, session tokens, internal flags. The fields to strip are configured as dot paths, and a * segment matches every entry at that level, so one rule can clean every item of a list.
Implement omitPaths(value, paths). It returns a copy of value with every location matched by a path removed.
omitPaths(value, paths) // => a new value
., for example 'password', 'profile.address.zip' or 'sessions.*.token'.'0', '12'). A numeric segment such as '2024' also matches an object key '2024'.* matches every key of an object and every index of an array.['tags.1', 'tags.3'] on ['a', 'b', 'c', 'd'] leaves ['a', 'c'].'a' and 'a.b' are fine.Object.prototype or null) are traversed. Every array and plain object in the result is a new copy, even if nothing inside it was removed.Date, Map, class instances, functions) is kept as is, by reference, and never looked inside.0, '', null and undefined.value itself is not an array or plain object, return it unchanged.const response = {
id: 'u_42',
password: 'hunter2',
profile: { name: 'Mina', address: { city: 'Oslo', zip: '0150' } },
sessions: [
{ device: 'ios', token: 't1' },
{ device: 'web', token: 't2' },
],
};
omitPaths(response, ['password', 'profile.address.zip', 'sessions.*.token']);
// => {
// id: 'u_42',
// profile: { name: 'Mina', address: { city: 'Oslo' } },
// sessions: [{ device: 'ios' }, { device: 'web' }],
// }
omitPaths({ tags: ['a', 'b', 'c', 'd'] }, ['tags.1', 'tags.3']); // => { tags: ['a', 'c'] }
omitPaths([{ a: 1, b: 2 }, { a: 3 }], ['*.a']); // => [{ b: 2 }, {}]
omitPaths({ when: new Date(0) }, ['when.getTime']); // => { when: (the same Date) }
omitPaths(5, ['a']); // => 5