From bccd364a4939cdc1e1d43b88bda42bff0660473b Mon Sep 17 00:00:00 2001 From: Abraham Hernandez Date: Fri, 26 Jul 2019 00:45:18 -0400 Subject: [PATCH] add type definitions --- .gitattributes | 1 + .github/funding.yml | 3 +++ .gitignore | 66 ++------------------------------------------- .npmrc | 1 + index.d.ts | 0 index.js | 18 +++++++------ index.test-d.ts | 0 package.json | 30 +++++++++------------ test.js | 21 ++++++++++----- 9 files changed, 43 insertions(+), 97 deletions(-) create mode 100644 .gitattributes create mode 100644 .github/funding.yml create mode 100644 .npmrc create mode 100644 index.d.ts create mode 100644 index.test-d.ts diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..94f480d --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf \ No newline at end of file diff --git a/.github/funding.yml b/.github/funding.yml new file mode 100644 index 0000000..be57bba --- /dev/null +++ b/.github/funding.yml @@ -0,0 +1,3 @@ +patreon: abranhe +open_collective: abranhe +custom: https://cash.me/$abranhe \ No newline at end of file diff --git a/.gitignore b/.gitignore index 4e09112..f483fc1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,65 +1,3 @@ -# Logs -logs -*.log -npm-debug.log* -yarn-debug.log* -yarn-error.log* -# Runtime data -pids -*.pid -*.seed -*.pid.lock - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage - -# nyc test coverage -.nyc_output - -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# Bower dependency directory (https://bower.io/) -bower_components - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (https://nodejs.org/api/addons.html) -build/Release -build - -# Dependency directories -node_modules/ -jspm_packages/ - -# TypeScript v1 declaration files -typings/ - -# Optional npm cache directory -.npm - -# Optional eslint cache -.eslintcache - -# Optional REPL history -.node_repl_history - -# Output of 'npm pack' -*.tgz -package-lock.json - -# Yarn Integrity file -.yarn-integrity - -# dotenv environment variables file -.env - -# next.js build output -.next - -*.DS_Store +node_modules +yarn.lock \ No newline at end of file diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..9cf9495 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false \ No newline at end of file diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..e69de29 diff --git a/index.js b/index.js index 6d6d2d3..adf111b 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,14 @@ 'use strict'; -module.exports = (str) => { - const obj = {}; +module.exports = str => { + return str + .replace(/\s/g, '') + .split(',') + .map(key => { + const a = key.split(':')[0]; + const b = key.split(':')[1]; - str.replace(/\s/g, '').split(',').forEach(propName => { - const propVal = propName.split(':'); - obj[propVal[0]] = propVal[1] || null; - }); - - return obj; + return {[a]: isNaN(b) ? b : Number(b)}; + }) + .reduce((x, y) => ({...x, ...y})); }; diff --git a/index.test-d.ts b/index.test-d.ts new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json index a8b3087..8045ede 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,11 @@ { "name": "s-to-o", "version": "1.0.0", - "description": "STRING to OBJECT (s-to-o)", - "main": "index.js", + "description": "Convert an string to an object", "scripts": { - "test": "ava && ci-success travis --success" - }, - "repository": { - "type": "git", - "url": "https://github.com/abranhe/s-to-o.git" + "test": "ava && xo && tsd" }, + "repository": "abranhe/s-to-o", "keywords": [ "string", "object", @@ -19,20 +15,18 @@ "string-to" ], "files": [ - "index.js" + "index.js", + "index.d.ts" ], "author": { - "name": "Carlos Abraham", - "email": "abraham@abranhe.com", - "web": "abranhe.com" - }, + "name": "Abraham Hernandez", + "email": "abraham@abranhe.com", + "url": "abranhe.com" + }, "license": "MIT", - "bugs": { - "url": "https://github.com/abranhe/s-to-o/issues" - }, - "homepage": "https://github.com/abranhe/s-to-o#readme", "devDependencies": { - "ci-success": "^2.0.0", - "ava": "1.0.0-beta.6" + "ava": "*", + "tsd": "^0.7.3", + "xo": "*" } } diff --git a/test.js b/test.js index 31f59c6..7f423df 100644 --- a/test.js +++ b/test.js @@ -1,13 +1,20 @@ import test from 'ava'; -const sTo = require('./'); +import sTo from '.'; -const obj = { - a: '1', - b: '2', - c: '3', - d: '4' +const objString = 'a: a, b: b, c: 3, d: 7'; + +const fixture = { + a: 'a', + b: 'b', + c: 3, + d: 7 }; test('Test string to object', t => { - t.truthy(sTo('a: 1, b: 2, c: 3, d: 4'), obj); + t.is(typeof sTo(objString), 'object'); + t.is(sTo(objString).a, fixture.a); + t.is(sTo(objString).b, fixture.b); + t.is(sTo(objString).c, fixture.c); + t.is(sTo(objString).d, fixture.d); + t.not(sTo(objString).c, `${fixture.c}`); });