Skip to content

Commit 102cc6b

Browse files
committed
1.0.1
+ Prototype Pollution fix + dependencies are up to date
1 parent 2e246ec commit 102cc6b

File tree

7 files changed

+2839
-3951
lines changed

7 files changed

+2839
-3951
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: node_js
22
node_js:
3-
- "8"
4-
- "6"
3+
- "12"
4+
- "10"
55
before_script:
66
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
77
- chmod +x ./cc-test-reporter

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2018 Alexey Bystrov
3+
Copyright (c) 2018-present Alexey Bystrov
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
set [![License](https://img.shields.io/npm/l/@strikeentco/set.svg)](https://github.com/strikeentco/set/blob/master/LICENSE) [![npm](https://img.shields.io/npm/v/@strikeentco/set.svg)](https://www.npmjs.com/package/@strikeentco/set)
22
==========
3-
[![Build Status](https://travis-ci.org/strikeentco/set.svg)](https://travis-ci.org/strikeentco/set) [![node](https://img.shields.io/node/v/@strikeentco/set.svg)](https://www.npmjs.com/package/@strikeentco/set) [![Test Coverage](https://api.codeclimate.com/v1/badges/450e530044d31f690dc5/test_coverage)](https://codeclimate.com/github/strikeentco/set/test_coverage) [![bitHound Score](https://www.bithound.io/github/strikeentco/set/badges/score.svg)](https://www.bithound.io/github/strikeentco/set)
3+
[![Build Status](https://travis-ci.org/strikeentco/set.svg)](https://travis-ci.org/strikeentco/set) [![node](https://img.shields.io/node/v/@strikeentco/set.svg)](https://www.npmjs.com/package/@strikeentco/set) [![Test Coverage](https://api.codeclimate.com/v1/badges/450e530044d31f690dc5/test_coverage)](https://codeclimate.com/github/strikeentco/set/test_coverage)
44

5-
One of the smallest (*24 sloc*) and most effective implementations of setting a nested value on an object.
5+
One of the smallest (*31 sloc*) and most effective implementations of setting a nested value on an object.
66

77
# Usage
88

@@ -41,4 +41,4 @@ set({ a: { b: 'c' } }, 'a:b', 'd', ':');
4141
## License
4242

4343
The MIT License (MIT)<br/>
44-
Copyright (c) 2018 Alexey Bystrov
44+
Copyright (c) 2018-present Alexey Bystrov

main.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11
'use strict';
22

3-
const isObject = val => typeof val === 'object' || typeof val === 'function';
3+
/* eslint-disable no-continue */
4+
5+
const isObject = (val) => typeof val === 'object' || typeof val === 'function';
6+
const isProto = (val, obj) => val === '__proto__' || (val === 'constructor' && typeof obj.constructor === 'function');
47
const set = (obj, parts, length, val) => {
58
let tmp = obj;
69
let i = 0;
710
for (; i < length - 1; i++) {
811
const part = parts[i];
12+
if (isProto(part, tmp)) {
13+
continue;
14+
}
915
tmp = !isObject(tmp[part]) ? tmp[part] = {} : tmp[part];
1016
}
1117
tmp[parts[i]] = val;
1218
return obj;
1319
};
1420

21+
/**
22+
* Sets nested values on an object using a dot path or custom separator
23+
* @param {Object} obj
24+
* @param {String|Array} path
25+
* @param {Any} val
26+
* @param {String} [sep = '.']
27+
* @returns {Object}
28+
*/
1529
module.exports = (obj, path, val, sep = '.') => {
1630
if (!isObject(obj) || !path || !path.length) {
1731
return obj;
1832
}
1933
const parts = Array.isArray(path) ? path : String(path).split(sep);
34+
if (isProto(parts[0], obj)) {
35+
return obj;
36+
}
2037
const { length } = parts;
2138
if (length === 1) {
2239
obj[parts[0]] = val;

0 commit comments

Comments
 (0)