Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions docs/modules/dot-prop.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,18 @@ setProperty(obj, 'foo.bar.baz', 'value') // [!code --]
dset(obj, 'foo.bar.baz', 'value') // [!code ++]
```

## `object-path`
## `String.prototype.split` + `Array.prototype.reduce` (native)

[`object-path`](https://github.com/mariocasciaro/object-path) provides get/set/has/delete operations plus array methods like push, insert, and empty.
If you only need to get a nested value you can use a simple function:

```ts
import { deleteProperty, getProperty, hasProperty, setProperty } from 'dot-prop' // [!code --]
import objectPath from 'object-path' // [!code ++]

const value = getProperty(obj, 'foo.bar.baz') // [!code --]
const value = objectPath.get(obj, 'foo.bar.baz') // [!code ++]
```js
function getProperty(obj, key) {
return key.split('.').reduce((acc, k) => acc?.[k], obj)
}

setProperty(obj, 'foo.bar.baz', 'value') // [!code --]
objectPath.set(obj, 'foo.bar.baz', 'value') // [!code ++]

const exists = hasProperty(obj, 'foo.bar.baz') // [!code --]
const exists = objectPath.has(obj, 'foo.bar.baz') // [!code ++]

deleteProperty(obj, 'foo.bar.baz') // [!code --]
objectPath.del(obj, 'foo.bar.baz') // [!code ++]
const value = getProperty(obj, 'foo.bar.baz')
```

> [!NOTE]
> This assumes that you do not consume dot paths as user input.
> If you do, ensure you sanitize keys before accessing them (e.g. through `Object.hasOwn`).
26 changes: 16 additions & 10 deletions manifests/preferred.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
"dot-prop": {
"type": "module",
"moduleName": "dot-prop",
"replacements": ["dlv", "object-path"],
"replacements": ["dlv"],
"url": {"type": "e18e", "id": "dot-prop"}
},
"dotenv": {
Expand All @@ -192,6 +192,12 @@
"replacements": ["--env-file"],
"url": {"type": "e18e", "id": "dotenv"}
},
"dottie": {
"type": "module",
"moduleName": "dottie",
"replacements": ["dlv", "dset"],
"url": {"type": "e18e", "id": "dot-prop"}
},
"duplexer": {
"type": "module",
"moduleName": "duplexer",
Expand Down Expand Up @@ -345,7 +351,7 @@
"get-value": {
"type": "module",
"moduleName": "get-value",
"replacements": ["dlv", "object-path"],
"replacements": ["dlv"],
"url": {"type": "e18e", "id": "dot-prop"}
},
"glob": {
Expand Down Expand Up @@ -2427,7 +2433,7 @@
"object-path": {
"type": "module",
"moduleName": "object-path",
"replacements": ["dlv", "object-path"],
"replacements": ["dlv", "dset"],
"url": {"type": "e18e", "id": "dot-prop"}
},
"ora": {
Expand Down Expand Up @@ -2575,7 +2581,7 @@
"set-value": {
"type": "module",
"moduleName": "set-value",
"replacements": ["dlv", "object-path"],
"replacements": ["dset"],
"url": {"type": "e18e", "id": "dot-prop"}
},
"shortid": {
Expand Down Expand Up @@ -2982,6 +2988,12 @@
"url": {"type": "e18e", "id": "dot-prop"},
"replacementModule": "dlv"
},
"dset": {
"id": "dset",
"type": "documented",
"url": {"type": "e18e", "id": "dot-prop"},
"replacementModule": "dset"
},
"elysia": {
"id": "elysia",
"type": "documented",
Expand Down Expand Up @@ -3294,12 +3306,6 @@
"url": {"type": "e18e", "id": "npm-run-all"},
"replacementModule": "npm-run-all2"
},
"object-path": {
"id": "object-path",
"type": "documented",
"url": {"type": "e18e", "id": "dot-prop"},
"replacementModule": "object-path"
},
"obug": {
"id": "obug",
"type": "documented",
Expand Down
11 changes: 10 additions & 1 deletion scripts/validate-manifests.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,20 @@ export async function validateManifests() {
}

for (const replacementId of mapping.replacements) {
if (!manifest.replacements[replacementId]) {
const replacement = manifest.replacements[replacementId];

if (!replacement) {
throw new Error(
`${manifestPath}: mapping "${key}" references unknown replacement "${replacementId}"`
);
}

if (replacement.replacementModule === key) {
throw new Error(
`${manifestPath}: mapping "${key}" defines a replacementModule that is identical to itself.`
);
}

usedReplacementIds.add(replacementId);
}
}
Expand Down