Permalink
Browse files

Added Moment.js for propper Date conversions supported by the 'parse'…

… schema field options that can specify an optional date parting format
  • Loading branch information...
1 parent 2d6e4b4 commit 8e739d0fae72c83922c05d5af26046b2f7d9babe @gabipetrovay gabipetrovay committed Apr 9, 2014
Showing with 25 additions and 17 deletions.
  1. +9 −4 README.md
  2. +2 −2 lib/adapter.js
  3. +5 −2 lib/convert.js
  4. +4 −4 lib/default.js
  5. +3 −4 lib/manipulator.js
  6. +2 −1 package.json
View
13 README.md
@@ -85,20 +85,25 @@ Results of atomic operations are not validated.
</tr>
<tr>
<td><code>pre</code></td>
- <td><code>"pre"</code></td>
- <td></td>
+ <td><code>"my prefix "</code></td>
+ <td>A string to prefix the value</td>
</tr>
<tr>
<td><code>post</code></td>
- <td><code>"post"</code></td>
- <td></td>
+ <td><code>" my suffix"</code></td>
+ <td>A string to suffix the value</td>
</tr>
<tr>
<td><code>charStyle</code></td>
<td><code>"normal"</code></td>
<td>Possible values: <code>"normal"</code>, <code>"uppercase"</code>, <code>"lowercase"</code></td>
</tr>
<tr>
+ <td><code>parse</code></td>
+ <td><code>DD.MM.YYY</code></td>
+ <td>For `date` types only. A string value passed if available to the Moment.js date parser.</td>
+ </tr>
+ <tr>
<td><code>trim</code></td>
<td><code>true</code></td>
<td>Boolean value. Trim or not?</td>
View
4 lib/adapter.js
@@ -126,7 +126,7 @@ function adapter (method, args, docIndex) {
for (var field in self._schema.default) {
if (typeof result[2][field] === 'undefined' && self._schema.paths[field]) {
- var value = defaultValue(self._schema.paths[field].type, self._schema.default[field]);
+ var value = defaultValue(self._schema.paths[field], self._schema.default[field]);
// handle default value errors
if (value.constructor.name === 'Error') {
@@ -159,4 +159,4 @@ function adapter (method, args, docIndex) {
return self.collection[method].apply(self.collection, args);
}
-module.exports = adapter;
+module.exports = adapter;
View
7 lib/convert.js
@@ -1,6 +1,9 @@
var ObjectId = require('pongo').ObjectId;
+var moment = require('moment');
-function convert (type, value) {
+function convert (options, value) {
+
+ var type = options.type;
if (typeof value === 'string') {
@@ -42,7 +45,7 @@ function convert (type, value) {
}
if (value && value.constructor.name !== 'Date' && type === 'date') {
- value = new Date(value);
+ value = moment(value, options.parse || undefined).toDate();
if (isNaN(value.valueOf())) {
return new Error('Date conversion failed:');
View
8 lib/default.js
@@ -12,13 +12,13 @@ var types = {
};
// TODO handle validation
-function handleValue (type, value) {
+function handleValue (options, value) {
- if (types[type]) {
- value = types[type](value);
+ if (types[options.type]) {
+ value = types[options.type](value);
}
- return convert(type, value);
+ return convert(options, value);
}
module.exports = handleValue;
View
7 lib/manipulator.js
@@ -53,7 +53,7 @@ var manipulators = {
}
return ['Validation min failed'];
- },
+ }
}
// adapter (return document)
@@ -62,7 +62,7 @@ module.exports = function (options, value, path) {
var result;
// convert type
- value = convert(options.type, value);
+ value = convert(options, value);
if (value && value.constructor.name === 'Error') {
return [value.toString()];
}
@@ -101,7 +101,6 @@ module.exports = function (options, value, path) {
if (typeof options.validate === 'function') {
if (!options.validate(value)) {
- debugger;
return ['Schema validation failed: ' + path + ' expected ' + options.type + " but got " + (value ? value.toString() : value)];
}
@@ -113,4 +112,4 @@ module.exports = function (options, value, path) {
}
return [null, value];
-};
+};
View
3 package.json
@@ -21,7 +21,8 @@
"mapper"
],
"dependencies" : {
- "pongo": "*"
+ "pongo": "*",
+ "moment": "*"
},
"engine": {
"node": ">=0.8"

0 comments on commit 8e739d0

Please sign in to comment.