Skip to content

Commit

Permalink
take an object instead than an array for social networks to check ava…
Browse files Browse the repository at this point in the history
…ilability
  • Loading branch information
Mirco Cipriani committed Jun 6, 2016
1 parent 4303968 commit 55907e4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,18 @@ Return a list of social networks application installed on user device

Options key|Description|Example
--- | --- | ---
*socials* | Array of strings: social network to check if available (chooser, facebook, twitter, whatsapp) | chooser
*socials* | Object with socials to check if available (facebook, twitter, whatsapp) | {"facebook": true, "twitter": true, "instagram": false }
*url* | String: url to share | "http://www.google.com/?utm_source=stargate"

### Returns

Promise fullfilled with an array with the list social network available from the ones requested with parameter "option.socials"
Promise fullfilled with an object with social networks availablility from the ones requested with parameter "option.socials"
For example:
{
"facebook": true,
"twitter": true,
"instagram": false
}


## Stargate.facebookShare(url, callbackSuccess, callbackError)
Expand Down
17 changes: 13 additions & 4 deletions spec/stargate.share.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ describe("Stargate share", function() {

res.catch(function(message) {
//console.log("stargatePublic.socialShare catch: "+message);
expect(message).toMatch(/missing array parameter socials/);
expect(message).toMatch(/missing object parameter socials/);
done();
});
});
Expand All @@ -261,9 +261,18 @@ describe("Stargate share", function() {

var options = {
"url": "http://www.google.com",
"socials": ["facebook", "twitter", "instagram"]
"socials": {
"facebook": true,
"twitter": true,
"instagram": false
}
};

var expectedResult = {};
for (var key in options.socials) {
if (options.socials[key]) {
expectedResult[key] = true;
}
}
var res = stargatePublic.socialShareAvailable(options);

expect(res.then).toBeDefined();
Expand All @@ -277,7 +286,7 @@ describe("Stargate share", function() {
res.then(function(result) {
//console.log("stargatePublic.socialShareAvailable result: "+result);
expect(result).not.toBeFalsy();
expect(result).toEqual(options.socials);
expect(result).toEqual(expectedResult);
done();
});
});
Expand Down
14 changes: 6 additions & 8 deletions src/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ stargatePublic.socialShareAvailable = function(options) {
return Promise.reject("missing cordova plugin");
}

if (!options.socials || options.socials.constructor !== Array) {
err("[share] missing array parameter socials");
return Promise.reject("missing array parameter socials");
if (!options.socials || typeof options.socials !== "object") {
err("[share] missing object parameter socials");
return Promise.reject("missing object parameter socials");
}

if (!options.url) {
Expand All @@ -269,7 +269,7 @@ stargatePublic.socialShareAvailable = function(options) {
knownSocialNetworks.forEach(function(element) {
// check only requested networks

if (options.socials.indexOf(element) !== -1) {
if (options.socials[element]) {

socialsAvailabilityPromises.push(

Expand All @@ -281,14 +281,12 @@ stargatePublic.socialShareAvailable = function(options) {

Promise.all(socialsAvailabilityPromises).then(function(values) {

var availableNetworks = [];
var availableNetworks = {};
// values is like:
// [{"network": "facebook", "available": false},
// {"network": "twitter", "available": false}]
values.forEach(function(element) {
if (element.available) {
availableNetworks.push(element.network);
}
availableNetworks[element.network] = element.available;
//log("element: ", element);
});
//log("values: ", values);
Expand Down

0 comments on commit 55907e4

Please sign in to comment.