Repository: cordova-plugin-contacts Updated Branches: refs/heads/master fee446e44 -> 4774ee979
CB-8115 incorrect birthday saved to phonebook using Contacts Plugin Convert birthday to Date when picking a contact Added a corresponding test for find method Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/commit/4774ee97 Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/tree/4774ee97 Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/diff/4774ee97 Branch: refs/heads/master Commit: 4774ee97911f9c3a6b072e9cf44c7bfc6c5fb7ff Parents: fee446e Author: daserge <[email protected]> Authored: Wed Mar 30 19:16:48 2016 +0300 Committer: Sergey Shakhnazarov <[email protected]> Committed: Tue Apr 5 12:57:03 2016 +0300 ---------------------------------------------------------------------- plugin.xml | 3 +++ tests/tests.js | 19 +++++++++++++++ www/Contact.js | 45 ++++------------------------------- www/contacts.js | 11 +++++---- www/convertUtils.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 46 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/4774ee97/plugin.xml ---------------------------------------------------------------------- diff --git a/plugin.xml b/plugin.xml index f1d5891..f283679 100644 --- a/plugin.xml +++ b/plugin.xml @@ -41,6 +41,9 @@ <clobbers target="Contact" /> </js-module> + <js-module src="www/convertUtils.js" name="convertUtils"> + </js-module> + <js-module src="www/ContactAddress.js" name="ContactAddress"> <clobbers target="ContactAddress" /> </js-module> http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/4774ee97/tests/tests.js ---------------------------------------------------------------------- diff --git a/tests/tests.js b/tests/tests.js index 1089501..1155a77 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -741,6 +741,25 @@ exports.defineAutoTests = function() { saveAndFindBy(contact, ["phoneNumbers"], "555-555-1234", done, this); }, MEDIUM_TIMEOUT); + + it("contacts.spec.31 Find should return a contact with correct birthday field type", function(done) { + // Save method is not supported on Windows platform + if (isWindows || isWindowsPhone8 || isIOSPermissionBlocked) { + pending(); + } + var contactName = "DeleteMe"; + var bDay = new Date(1976, 7, 4); + var contact = new Contact(); + contact.name = new ContactName(); + contact.name.familyName = contactName; + contact.note = "DeleteMe"; + contact.birthday = bDay; + saveAndFindBy(contact, ["displayName", "name"], contactName, function(found) { + expect(found.birthday).toEqual(jasmine.any(Date)); + expect(found.birthday).toEqual(bDay); + done(); + }, done, this); + }, MEDIUM_TIMEOUT); }); describe('ContactError interface', function() { http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/4774ee97/www/Contact.js ---------------------------------------------------------------------- diff --git a/www/Contact.js b/www/Contact.js index 9c46a0c..26f7420 100644 --- a/www/Contact.js +++ b/www/Contact.js @@ -22,45 +22,8 @@ var argscheck = require('cordova/argscheck'), exec = require('cordova/exec'), ContactError = require('./ContactError'), - utils = require('cordova/utils'); - -/** -* Converts primitives into Complex Object -* Currently only used for Date fields -*/ -function convertIn(contact) { - var value = contact.birthday; - try { - contact.birthday = new Date(parseFloat(value)); - } catch (exception){ - console.log("Cordova Contact convertIn error: exception creating date."); - } - return contact; -} - -/** -* Converts Complex objects into primitives -* Only conversion at present is for Dates. -**/ - -function convertOut(contact) { - var value = contact.birthday; - if (value !== null) { - // try to make it a Date object if it is not already - if (!utils.isDate(value)){ - try { - value = new Date(value); - } catch(exception){ - value = null; - } - } - if (utils.isDate(value)){ - value = value.valueOf(); // convert to milliseconds - } - contact.birthday = value; - } - return contact; -} + utils = require('cordova/utils'), + convertUtils = require('./convertUtils'); /** * Contains information about a single contact. @@ -161,7 +124,7 @@ Contact.prototype.save = function(successCB, errorCB) { if (result) { if (successCB) { var fullContact = require('./contacts').create(result); - successCB(convertIn(fullContact)); + successCB(convertUtils.toCordovaFormat(fullContact)); } } else { @@ -169,7 +132,7 @@ Contact.prototype.save = function(successCB, errorCB) { fail(ContactError.UNKNOWN_ERROR); } }; - var dupContact = convertOut(utils.clone(this)); + var dupContact = convertUtils.toNativeFormat(utils.clone(this)); exec(success, fail, "Contacts", "save", [dupContact]); }; http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/4774ee97/www/contacts.js ---------------------------------------------------------------------- diff --git a/www/contacts.js b/www/contacts.js index b1b2711..cd0df81 100644 --- a/www/contacts.js +++ b/www/contacts.js @@ -23,7 +23,8 @@ var argscheck = require('cordova/argscheck'), exec = require('cordova/exec'), ContactError = require('./ContactError'), Contact = require('./Contact'), - fieldType = require('./ContactFieldType'); + fieldType = require('./ContactFieldType'), + convertUtils = require('./convertUtils'); /** * Represents a group of Contacts. @@ -39,7 +40,7 @@ var contacts = { * @param {ContactFindOptions} options that can be applied to contact searching * @return array of Contacts matching search criteria */ - find:function(fields, successCB, errorCB, options) { + find: function(fields, successCB, errorCB, options) { argscheck.checkArgs('afFO', 'contacts.find', arguments); if (!fields.length) { if (errorCB) { @@ -51,7 +52,7 @@ var contacts = { var win = function(result) { var cs = []; for (var i = 0, l = result.length; i < l; i++) { - cs.push(contacts.create(result[i])); + cs.push(convertUtils.toCordovaFormat(contacts.create(result[i]))); } successCB(cs); }; @@ -71,7 +72,7 @@ var contacts = { // if Contacts.pickContact return instance of Contact object // don't create new Contact object, use current var contact = result instanceof Contact ? result : contacts.create(result); - successCB(contact); + successCB(convertUtils.toCordovaFormat(contact)); }; exec(win, errorCB, "Contacts", "pickContact", []); }, @@ -83,7 +84,7 @@ var contacts = { * @param properties an object whose properties will be examined to create a new Contact * @returns new Contact object */ - create:function(properties) { + create: function(properties) { argscheck.checkArgs('O', 'contacts.create', arguments); var contact = new Contact(); for (var i in properties) { http://git-wip-us.apache.org/repos/asf/cordova-plugin-contacts/blob/4774ee97/www/convertUtils.js ---------------------------------------------------------------------- diff --git a/www/convertUtils.js b/www/convertUtils.js new file mode 100644 index 0000000..5d997d8 --- /dev/null +++ b/www/convertUtils.js @@ -0,0 +1,61 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +var utils = require('cordova/utils'); + +module.exports = { + /** + * Converts primitives into Complex Object + * Currently only used for Date fields + */ + toCordovaFormat: function (contact) { + var value = contact.birthday; + try { + contact.birthday = new Date(parseFloat(value)); + } catch (exception){ + console.log("Cordova Contact toCordovaFormat error: exception creating date."); + } + return contact; + }, + + /** + * Converts Complex objects into primitives + * Only conversion at present is for Dates. + **/ + toNativeFormat: function (contact) { + var value = contact.birthday; + if (value !== null) { + // try to make it a Date object if it is not already + if (!utils.isDate(value)){ + try { + value = new Date(value); + } catch(exception){ + value = null; + } + } + if (utils.isDate(value)){ + value = value.valueOf(); // convert to milliseconds + } + contact.birthday = value; + } + return contact; + } +}; --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
