├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── SECURITY.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # generate-object-property 2 | 3 | Generate safe JS code that can used to reference a object property 4 | 5 | npm install generate-object-property 6 | 7 | ## Usage 8 | 9 | ``` js 10 | var gen = require('generate-object-property'); 11 | console.log(gen('a','b')); // prints a.b 12 | console.log(gen('a', 'foo-bar')); // prints a["foo-bar"] 13 | ``` 14 | 15 | ## License 16 | 17 | MIT 18 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | ## Security contact information 2 | 3 | To report a security vulnerability, please use the 4 | [Tidelift security contact](https://tidelift.com/security). 5 | Tidelift will coordinate the fix and disclosure. 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const isProperty = require('is-property') 2 | 3 | gen.valid = isProperty 4 | 5 | gen.property = function (prop) { 6 | return isProperty(prop) ? prop : JSON.stringify(prop) 7 | } 8 | 9 | gen.optional = function (obj, prop) { 10 | return isProperty(prop) ? obj + '?.' + prop : obj + '?.[' + JSON.stringify(prop) + ']' 11 | } 12 | 13 | module.exports = gen 14 | 15 | function gen (obj, prop) { 16 | return isProperty(prop) ? obj + '.' + prop : obj + '[' + JSON.stringify(prop) + ']' 17 | } 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "generate-object-property", 3 | "version": "2.0.0", 4 | "description": "Generate safe JS code that can used to reference a object property", 5 | "repository": { 6 | "type": "git", 7 | "url": "https://github.com/mafintosh/generate-object-property" 8 | }, 9 | "devDependencies": { 10 | "brittle": "^3.7.0" 11 | }, 12 | "scripts": { 13 | "test": "brittle test.js" 14 | }, 15 | "dependencies": { 16 | "is-property": "^1.0.0" 17 | }, 18 | "bugs": { 19 | "url": "https://github.com/mafintosh/generate-object-property/issues" 20 | }, 21 | "homepage": "https://github.com/mafintosh/generate-object-property", 22 | "main": "index.js", 23 | "author": "Mathias Buus (@mafintosh)", 24 | "license": "MIT" 25 | } 26 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | const test = require('brittle') 2 | const gen = require('./') 3 | 4 | test('valid', function (t) { 5 | t.is(gen('a', 'b'), 'a.b') 6 | }) 7 | 8 | test('invalid', function (t) { 9 | t.is(gen('a', '-b'), 'a["-b"]') 10 | }) 11 | 12 | test('valid (optional)', function (t) { 13 | t.is(gen.optional('a', 'b'), 'a?.b') 14 | }) 15 | 16 | test('invalid (optional)', function (t) { 17 | t.is(gen.optional('a', '-b'), 'a?.["-b"]') 18 | }) 19 | --------------------------------------------------------------------------------