Skip to content

Commit

Permalink
add type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
abranhe committed Jul 26, 2019
1 parent 2b3c35f commit bccd364
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 97 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
3 changes: 3 additions & 0 deletions .github/funding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
patreon: abranhe
open_collective: abranhe
custom: https://cash.me/$abranhe
66 changes: 2 additions & 64 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
Empty file added index.d.ts
Empty file.
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -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}));
};
Empty file added index.test-d.ts
Empty file.
30 changes: 12 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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": "*"
}
}
21 changes: 14 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
@@ -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}`);
});

0 comments on commit bccd364

Please sign in to comment.