Skip to content

Commit

Permalink
ES6 and Node 6 - Version 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mweibel authored and Michael Weibel committed Sep 5, 2016
1 parent 4c6b2c6 commit 60df296
Show file tree
Hide file tree
Showing 20 changed files with 371 additions and 351 deletions.
4 changes: 4 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["es2015"],
"plugins": ["transform-runtime"]
}
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage/
coverage/
dist/
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
env:
node: true
mocha: true
es6: true


rules:
no-undef: 2
Expand All @@ -14,7 +16,7 @@ rules:
no-extra-semi: 2
no-trailing-spaces: 2
semi: 2
max-len: [2, 100, 4]
max-len: [2, 120, 4]
comma-style: [2, "last", { exceptions: { VariableDeclaration: true } }]
comma-spacing: 2
consistent-this: [2, "self"]
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
._*
node_modules/
coverage/
coverage/
dist/
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
examples/
lib/
test/
prepublish.js
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v6
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ env:
- secure: i+a2mN2VhA1aOjrVzzMYDFqI4pFjELXdLs3S2Mx1JAyYWVQ/YNTKx4PExxN4I7JSdOo/wUNFKvWAHmnlkG6yzqVW+pLVi/XvUUL3B7WMGMgl5LbVmt/4wsrElv2NjtErRMwyCxWbkzYoegIm4hLuoC6/D/Hst/yGPMfsgvxMdNEVj5cOCS7P8F9U6KAoQ5XRJH1pvfrSbAiQ4/ZV0SqBj50WQE/S+szlJgVAcl+2sn/TdegZi8N5HuvNvasrgFSHSkxNssaivQIDLEW81wOv8euRGyZxtysJFbxfriuNKdKipwoexY2+PMHRRMgNdQjYwNLpdRmSSBtzMxQc6MJbIkragNwY9RnM1JyJstcIubt0OaKqvHWCLx3n5xVqbvDhn+ispFG4/Bd58R7XhHyHuhfOrrea3KF7OBOJfGBMG8w5rgjCRMbnTUyFkKJW3h/UN6MjIWPH+Tl2QdJcDhDRKwzoZcNpGiGmydIubaz9BCNni5im+1Da0MEhSaXzjl1aw+SaQaC3Ls7HcbAjAhrpZ1wt2cos2sLJ/yk+AQ3dnOkplhgxfBsbW+l+km0gR105/R76qbmSc6/ByQnG/DSfD5TZkmR1Ig8PcQ9AX9YxKBdbvEO5WLBwySy+6TUmEzktJY7nxmkK10HKPu9Rx9jE6bmVnVXtVD+AIi15a4GM0co=
language: node_js
node_js:
- 6
- 4
- 0.12
script: npm test
after_script: npm run coveralls
#addons:
Expand Down
34 changes: 23 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

Lightweight, extensible data transfer object (DTO) library for Node.js and browser environments.

Please note that this library is currently not running on Node.js >= 5.x due to compatibility issues with the new class inheritance model.

## Install

```bash
Expand All @@ -13,25 +11,28 @@ $ npm install dtox --save
## Usage

```js
var dtox = require('dtox')
, fields = dtox.fields;
const { BaseDTO, BaseListDTO, fields } = require('dtox')

// Define user mapping
var USER_MAPPING = {
const USER_MAPPING = {
id: fields.number(),
name: fields.string(),
validated: fields.boolean({ default: false }),
roles: fields.list({ default: [], key: 'groups' }),
hasRoles: fields.generic({ callback: function(data) {
hasRoles: fields.generic({ callback: (data) => {
return data.groups.length > 0;
}}),
dateCreated: fields.date()
};

// Define a DTO which represents a single user
var UserDTO = dtox.BaseDTO.inherit(USER_MAPPING);
class UserDTO extends BaseDTO {
constructor(data) {
super(data, USER_MAPPING);
}
}

var user = new UserDTO({
const user = new UserDTO({
id: 123,
name: 'john_doe',
validated: true,
Expand All @@ -43,9 +44,13 @@ console.log('Hello ' + user.name); // "Hello john_doe"
console.log('User ' + user.hasRoles ? 'has roles' : 'has no roles'); // "User has roles"

// Define a DTO which represents a list of users
var UserListDTO = dtox.BaseListDTO.inherit(UserDTO);
class UserListDTO extends BaseListDTO {
constructor(data) {
super(data, UserDTO);
}
}

var users = new UserListDTO([
const users = new UserListDTO([
{
id: 123,
name: 'john_doe',
Expand All @@ -62,9 +67,16 @@ var users = new UserListDTO([
}
]);

var userNames = users.map(function(user) {
const userNames = users.map((user) => {
return user.name;
});

console.log(userNames.join(', ')); // "john_doe, jane_doe"

// or also possible:
for (const userName of users) {
console.log(userName);
}

console.log(JSON.stringify(users)); // will print the items in JSON form
```
30 changes: 30 additions & 0 deletions coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Runs coverage
*/
'use strict';

const fs = require('fs');
const rimraf = require('rimraf');
const { execSync } = require('child_process');

console.log('Executing coverage.js');

const packageJson = JSON.parse(fs.readFileSync('./package.json'));
packageJson.main = 'index.js';

fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2));

console.log('* Updated package.json');

rimraf('./dist', () => {
console.log('* Removed ./dist folder');

execSync('./node_modules/.bin/babel-istanbul cover --report lcovonly -x \'**/examples/**\'' +
' ./node_modules/.bin/_mocha -- -R spec');

console.log('* Coverage report created');

execSync('git checkout -- package.json');

console.log('* Reverted change to package.json');
});
30 changes: 22 additions & 8 deletions examples/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';

var dtox = require('..')
, fields = dtox.fields;
const { BaseDTO, BaseListDTO, fields} = require('..');

// Define user mapping
var USER_MAPPING = {
const USER_MAPPING = {
id: fields.number(),
name: fields.string(),
roles: fields.list({ default: [], key: 'groups' }),
Expand All @@ -13,9 +12,13 @@ var USER_MAPPING = {
};

// Define a DTO which represents a single user
var UserDTO = dtox.BaseDTO.inherit(USER_MAPPING);
class UserDTO extends BaseDTO {
constructor(data) {
super(data, USER_MAPPING);
}
}

var user = new UserDTO({
const user = new UserDTO({
id: 123,
name: 'john_doe',
groups: ['administrator'],
Expand All @@ -26,9 +29,13 @@ var user = new UserDTO({
console.log('Hello ' + user.name); // "Hello john_doe"

// Define a DTO which represents a list of users
var UserListDTO = dtox.BaseListDTO.inherit(UserDTO);
class UserListDTO extends BaseListDTO {
constructor(data) {
super(data, UserDTO);
}
}

var users = new UserListDTO([
const users = new UserListDTO([
{
id: 123,
name: 'john_doe',
Expand All @@ -45,8 +52,15 @@ var users = new UserListDTO([
}
]);

var userNames = users.map(function(u) {
const userNames = users.map(function(u) {
return u.name;
});

console.log(userNames.join(', ')); // "john_doe, jane_doe"

// or also possible:
for (const userName of users) {
console.log(userName);
}

console.log(JSON.stringify(users)); // will print the items in JSON form
25 changes: 16 additions & 9 deletions examples/simple-list.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

// DTO definitions
var dtox = require('..')
, fields = dtox.fields;
const { BaseDTO, BaseListDTO, fields} = require('..');

var USER_MAPPING = {
const USER_MAPPING = {
id: fields.number(),
name: fields.string(),
username: fields.string(),
Expand All @@ -15,19 +13,28 @@ var USER_MAPPING = {
company: fields.generic()
};

var UserDTO = dtox.BaseDTO.inherit(USER_MAPPING);
var UserListDTO = dtox.BaseListDTO.inherit(UserDTO);
class UserDTO extends BaseDTO {
constructor(data) {
super(data, USER_MAPPING);
}
}

class UserListDTO extends BaseListDTO {
constructor(data) {
super(data, UserDTO);
}
}

// API call
var request = require('request-promise');
const request = require('request-promise');

request('http://jsonplaceholder.typicode.com/users', { json: true })
.then(function(res) {
var users = new UserListDTO(res);
const users = new UserListDTO(res);
users.forEach(function(user) {
console.log(user.id + ': ' + user.username);
});
})
.catch(function(err) {
console.error(err.stack);
});
});
18 changes: 10 additions & 8 deletions examples/simple.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
'use strict';

// DTO definitions
var dtox = require('..')
, fields = dtox.fields;
const { BaseDTO, fields} = require('..');

var USER_MAPPING = {
const USER_MAPPING = {
id: fields.number(),
name: fields.string(),
username: fields.string(),
Expand All @@ -15,16 +13,20 @@ var USER_MAPPING = {
company: fields.generic()
};

var UserDTO = dtox.BaseDTO.inherit(USER_MAPPING);
class UserDTO extends BaseDTO {
constructor(data) {
super(data, USER_MAPPING);
}
}

// API call
var request = require('request-promise');
const request = require('request-promise');

request('http://jsonplaceholder.typicode.com/users/1', { json: true })
.then(function(res) {
var user = new UserDTO(res);
const user = new UserDTO(res);
console.log(user.name);
})
.catch(function(err) {
console.error(err.stack);
});
});
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var lib = require('./lib/dto');
const lib = require('./lib/dto');

lib.fields = require('./lib/fields');
lib.errors = require('./lib/errors');

module.exports = lib;
module.exports = lib;

0 comments on commit 60df296

Please sign in to comment.