sfc-utils 1.4.92 → 1.4.94

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/personalize.js +50 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sfc-utils",
3
- "version": "1.4.92",
3
+ "version": "1.4.94",
4
4
  "author": "ewagstaff <evanjwagstaff@gmail.com>",
5
5
  "dependencies": {
6
6
  "archieml": "^0.4.2",
package/personalize.js CHANGED
@@ -36,14 +36,57 @@ const getProfileProperty = (property) => {
36
36
  });
37
37
  };
38
38
 
39
- const setProfileProperty = (property, value) => {
39
+ const setProfileProperty = (property, value, merge = false) => {
40
+ return new Promise((resolve, reject) => {
41
+ if (window) {
42
+ if (window.blueConicClient) {
43
+ // Prepare merging (note: this only handles objects, not arrays or other types)
44
+ let conditionalPromise = Promise.resolve();
45
+ if (merge && typeof value === "object") {
46
+ conditionalPromise = getProfileProperty(property);
47
+ }
48
+
49
+ conditionalPromise.then((existingValue) => {
50
+ // Finish merging
51
+ if (merge && typeof value === "object") {
52
+ console.log("Merging values...", JSON.parse(existingValue), value);
53
+ value = JSON.stringify(
54
+ Object.assign({}, JSON.parse(existingValue), value)
55
+ );
56
+ console.log("Merged value", value);
57
+ }
58
+ // Make sure property to save is a string
59
+ // If object, stringify it
60
+ if (typeof value === "object") {
61
+ value = JSON.stringify(value);
62
+ } else if (typeof value === "boolean" || typeof value === "number") {
63
+ value = value.toString();
64
+ }
65
+
66
+ const profile = window.blueConicClient.profile.getProfile();
67
+ const properties = [property];
68
+ profile.loadValues(properties, this, function () {
69
+ profile.setValue(property, value);
70
+ resolve(true);
71
+ });
72
+ });
73
+ } else {
74
+ resolve("Personalize: No BlueConic client found");
75
+ }
76
+ } else {
77
+ resolve("Personalize: No window found");
78
+ }
79
+ });
80
+ };
81
+
82
+ const clearProfileProperty = (property) => {
40
83
  return new Promise((resolve, reject) => {
41
84
  if (window) {
42
85
  if (window.blueConicClient) {
43
86
  const profile = window.blueConicClient.profile.getProfile();
44
87
  const properties = [property];
45
88
  profile.loadValues(properties, this, function () {
46
- profile.setValue(property, value);
89
+ profile.setValue(property, "");
47
90
  resolve(true);
48
91
  });
49
92
  } else {
@@ -55,4 +98,8 @@ const setProfileProperty = (property, value) => {
55
98
  });
56
99
  };
57
100
 
58
- module.exports = { getProfileProperty, setProfileProperty };
101
+ module.exports = {
102
+ getProfileProperty,
103
+ setProfileProperty,
104
+ clearProfileProperty,
105
+ };