mp-js-api 0.0.16 → 0.0.17

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.
package/.gitattributes CHANGED
@@ -1,2 +1,2 @@
1
- # Auto detect text files and perform LF normalization
2
- * text=auto
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
@@ -51,7 +51,7 @@ function toCamelCase(str, { capitalIds = false } = {}) {
51
51
  str = str.toLowerCase();
52
52
  // str = str.replace(/^_?[A-Z]{1,3}/, match => match.toLowerCase()); // Don't convert if start with ID, HS, SMS, etc
53
53
  str = str.replace(/(?<=^_|^__)[^\W_]/g, match => match.at(-1)?.toLowerCase() || ''); // keep underscore if first char
54
- str = str.replace(/(?<!^_|^)_[^\W_]/g, match => match.charAt(1).toUpperCase()); // remove underscore if not first char
54
+ str = str.replace(/(?<!^_|^)[^a-zA-Z0-9][^\W_]/g, match => match.charAt(1).toUpperCase()); // remove non-word char if not first char
55
55
  return capitalIds ? str.replace(/id$/i, 'ID') : str;
56
56
  }
57
57
  function toCapitalSnakeCase(str, { capitalIds = false, capitalSnake = true } = {}) {
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "mp-js-api",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "description": "Node MinistryPlatform API",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
7
  "test": "mocha --require ts-node/register test/*",
8
- "postinstall": "tsc"
8
+ "compile": "tsc"
9
9
  },
10
10
  "keywords": [],
11
11
  "author": "",
@@ -46,7 +46,7 @@ export function toCamelCase(str: string, { capitalIds = false }: { capitalIds?:
46
46
  str = str.toLowerCase();
47
47
  // str = str.replace(/^_?[A-Z]{1,3}/, match => match.toLowerCase()); // Don't convert if start with ID, HS, SMS, etc
48
48
  str = str.replace(/(?<=^_|^__)[^\W_]/g, match => match.at(-1)?.toLowerCase() || ''); // keep underscore if first char
49
- str = str.replace(/(?<!^_|^)_[^\W_]/g, match => match.charAt(1).toUpperCase()); // remove underscore if not first char
49
+ str = str.replace(/(?<!^_|^)[^a-zA-Z0-9][^\W_]/g, match => match.charAt(1).toUpperCase()); // remove non-word char if not first char
50
50
  return capitalIds ? str.replace(/id$/i, 'ID') : str;
51
51
  }
52
52
 
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=json-sql.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"json-sql.d.ts","sourceRoot":"","sources":["../../src/utils/json-sql.ts"],"names":[],"mappings":""}
@@ -1,111 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- function jsonToSqlFilter(json) {
4
- const conditions = [];
5
- // Loop through each key-value pair in the JSON object
6
- for (const [key, value] of Object.entries(json)) {
7
- let condition = '';
8
- // If the value is a string, wrap it in quotes
9
- if (typeof value === 'string') {
10
- condition = `${key} = '${value}'`;
11
- }
12
- // If the value is a number or boolean, no quotes are needed
13
- else if (typeof value === 'number' || typeof value === 'boolean') {
14
- condition = `${key} = ${value}`;
15
- }
16
- // If the value is an array, assume it represents an IN condition
17
- else if (Array.isArray(value)) {
18
- const valueList = value.map(val => typeof val === 'string' ? `'${val}'` : val).join(', ');
19
- condition = `${key} IN (${valueList})`;
20
- }
21
- // If the value is an object, we can assume it's a nested condition (e.g., for complex queries)
22
- else if (typeof value === 'object') {
23
- const subConditions = Object.entries(value).map(([subKey, subValue]) => {
24
- if (typeof subValue === 'string') {
25
- return `${subKey} = '${subValue}'`;
26
- }
27
- else {
28
- return `${subKey} = ${subValue}`;
29
- }
30
- }).join(' AND ');
31
- condition = `${key} (${subConditions})`;
32
- }
33
- conditions.push(condition);
34
- }
35
- // Join the conditions with 'AND'
36
- return conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
37
- }
38
- // Example Usage
39
- const json1 = {
40
- name: "John",
41
- age: 30,
42
- active: true,
43
- hobbies: ["reading", "gaming"],
44
- address: {
45
- city: "New York",
46
- zip: "10001"
47
- }
48
- };
49
- const query1 = jsonToSqlFilter(json1);
50
- console.log(query1);
51
- function jsonToSqlQuery(json) {
52
- const conditions = [];
53
- // Loop through each key-value pair in the JSON object
54
- for (const [key, value] of Object.entries(json)) {
55
- let condition = '';
56
- // If the value is an object (operator condition)
57
- if (typeof value === 'object' && value !== null) {
58
- const operatorConditions = [];
59
- let [operator, operatorValue] = [null, null];
60
- for ([operator, operatorValue] of Object.entries(value)) {
61
- switch (operator) {
62
- case "$eq":
63
- operatorConditions.push(`${key} = '${operatorValue}'`);
64
- break;
65
- case "$gt":
66
- operatorConditions.push(`${key} > ${operatorValue}`);
67
- break;
68
- case "$lt":
69
- operatorConditions.push(`${key} < ${operatorValue}`);
70
- break;
71
- case "$gte":
72
- operatorConditions.push(`${key} >= ${operatorValue}`);
73
- break;
74
- case "$lte":
75
- operatorConditions.push(`${key} <= ${operatorValue}`);
76
- break;
77
- case "$ne":
78
- operatorConditions.push(`${key} != '${operatorValue}'`);
79
- break;
80
- case "$in":
81
- const inValues = operatorValue.map((val) => typeof val === 'string' ? `'${val}'` : val).join(', ');
82
- operatorConditions.push(`${key} IN (${inValues})`);
83
- break;
84
- case "$like":
85
- operatorConditions.push(`${key} LIKE '%${operatorValue}%'`);
86
- break;
87
- default:
88
- throw new Error(`Unsupported operator: ${operator}`);
89
- }
90
- }
91
- // Join all conditions for a field with AND
92
- condition = operatorConditions.join(' AND ');
93
- }
94
- else {
95
- // For simple equality condition
96
- condition = `${key} = '${value}'`;
97
- }
98
- conditions.push(condition);
99
- }
100
- // Join the conditions with AND
101
- return conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
102
- }
103
- // Example Usage
104
- const json = {
105
- "age": { "$gt": 30 },
106
- "name": { "$eq": "John" },
107
- "status": { "$in": ["active", "pending"] },
108
- "address.city": { "$eq": "New York" }
109
- };
110
- const query = jsonToSqlQuery(json);
111
- console.log(query);
@@ -1,16 +0,0 @@
1
- export declare function stringifyURLParams<T = any>(mpOptions?: Record<string, T>): string;
2
- export declare function escapeSql(str: string): string;
3
- export declare function toCamelCase(str: string, { capitalIds }?: {
4
- capitalIds?: boolean;
5
- }): string;
6
- export declare function toCapitalSnakeCase(str: string, { capitalIds, capitalSnake }?: {
7
- capitalIds?: boolean;
8
- capitalSnake?: boolean;
9
- }): string;
10
- export declare function caseConverter<T>(obj: T, { type, capitalIds }: {
11
- type: 'toCamel' | 'toSnake';
12
- capitalIds?: boolean;
13
- }): any;
14
- export declare function convertToCamelCase<T = any, R = any>(obj: Partial<T>, capitalIds?: boolean): R;
15
- export declare function convertToSnakeCase<T = any, R = any>(obj: Partial<T>, capitalIds?: boolean): R;
16
- //# sourceMappingURL=strings.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"strings.d.ts","sourceRoot":"","sources":["../../src/utils/strings.ts"],"names":[],"mappings":"AAAA,wBAAgB,kBAAkB,CAAC,CAAC,GAAG,GAAG,EAAE,SAAS,GAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAM,UAS5E;AAGD,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,UA2BpC;AAID,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,UAAkB,EAAE,GAAE;IAAE,UAAU,CAAC,EAAE,OAAO,CAAC;CAAM,UAO7F;AAED,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE,UAAkB,EAAE,YAAmB,EAAE,GAAE;IAAE,UAAU,CAAC,EAAE,OAAO,CAAC;IAAC,YAAY,CAAC,EAAE,OAAO,CAAC;CAAM,UAKjJ;AAGD,wBAAgB,aAAa,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,UAAkB,EAAE,EAAE;IAAE,IAAI,EAAE,SAAS,GAAG,SAAS,CAAC;IAAC,UAAU,CAAC,EAAE,OAAO,CAAC;CAAE,OAkB5H;AAED,wBAAgB,kBAAkB,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,UAAU,UAAO,GAAG,CAAC,CAE1F;AAED,wBAAgB,kBAAkB,CAAC,CAAC,GAAG,GAAG,EAAE,CAAC,GAAG,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC,CAAC,EAAE,UAAU,UAAO,GAAG,CAAC,CAE1F"}
@@ -1,86 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stringifyURLParams = stringifyURLParams;
4
- exports.escapeSql = escapeSql;
5
- exports.toCamelCase = toCamelCase;
6
- exports.toCapitalSnakeCase = toCapitalSnakeCase;
7
- exports.caseConverter = caseConverter;
8
- exports.convertToCamelCase = convertToCamelCase;
9
- exports.convertToSnakeCase = convertToSnakeCase;
10
- function stringifyURLParams(mpOptions = {}) {
11
- return escapeSql(Object.entries(mpOptions).reduce((acc, [key, value]) => {
12
- if (!acc) {
13
- acc += `?$${key}=${value}`;
14
- }
15
- else {
16
- acc += `&$${key}=${value}`;
17
- }
18
- return acc;
19
- }, ''));
20
- }
21
- function escapeSql(str) {
22
- return str.replace(/%|(?<=\w)'(?=\w)/g, function (char) {
23
- switch (char) {
24
- case "\0":
25
- return "\\0";
26
- case "\x08":
27
- return "\\b";
28
- case "\x09":
29
- return "\\t";
30
- case "\x1a":
31
- return "\\z";
32
- case "\n":
33
- return "\\n";
34
- case "\r":
35
- return "\\r";
36
- case "%":
37
- return "%25";
38
- case "'":
39
- return "''";
40
- case "\"":
41
- case "\\":
42
- return "\\" + char; // prepends a backslash to backslash, percent,
43
- // and double/single quotes
44
- default:
45
- return char;
46
- }
47
- });
48
- }
49
- function toCamelCase(str, { capitalIds = false } = {}) {
50
- str = str.replace('-', '');
51
- str = str.toLowerCase();
52
- // str = str.replace(/^_?[A-Z]{1,3}/, match => match.toLowerCase()); // Don't convert if start with ID, HS, SMS, etc
53
- str = str.replace(/(?<=^_|^__)[^\W_]/g, match => match.at(-1)?.toLowerCase() || ''); // keep underscore if first char
54
- str = str.replace(/(?<!^_|^)_[^\W_]/g, match => match.charAt(1).toUpperCase()); // remove underscore if not first char
55
- return capitalIds ? str.replace(/id$/i, 'ID') : str;
56
- }
57
- function toCapitalSnakeCase(str, { capitalIds = false, capitalSnake = true } = {}) {
58
- str = str.replace(/(?<=^_|^__)[^\W_]/, match => match.at(0)?.toUpperCase() || '');
59
- str = str.replace(/(?<!_|\/)(ID|[A-Z]|\d)/g, match => `_${match}`);
60
- str = capitalSnake ? str.charAt(0).toUpperCase() + str.slice(1) : str;
61
- return capitalIds ? str.replace(/_id$/i, '_ID') : str;
62
- }
63
- // Function to recursively convert object keys to Capital_Snake_Case
64
- function caseConverter(obj, { type, capitalIds = false }) {
65
- const caseFn = type === 'toCamel' ? toCamelCase : toCapitalSnakeCase;
66
- if (Array.isArray(obj)) {
67
- return obj.map(val => caseConverter(val, { type })); // Recursively process each array element
68
- }
69
- if (obj !== null && typeof obj === 'object') {
70
- const snakeCaseObj = {};
71
- for (const key in obj) {
72
- if (obj.hasOwnProperty(key)) {
73
- const snakeCaseKey = caseFn(key, { capitalIds }); // Convert key to Snake_Case
74
- snakeCaseObj[snakeCaseKey] = caseConverter(obj[key], { type }); // Recursively process nested objects
75
- }
76
- }
77
- return snakeCaseObj;
78
- }
79
- return obj; // Return value if it's neither an array nor an object
80
- }
81
- function convertToCamelCase(obj, capitalIds = true) {
82
- return caseConverter(obj, { type: 'toCamel', capitalIds });
83
- }
84
- function convertToSnakeCase(obj, capitalIds = true) {
85
- return caseConverter(obj, { type: 'toSnake', capitalIds });
86
- }
@@ -1,132 +0,0 @@
1
- function jsonToSqlFilter(json: Record<string, any>): string {
2
- const conditions: string[] = [];
3
-
4
- // Loop through each key-value pair in the JSON object
5
- for (const [key, value] of Object.entries(json)) {
6
- let condition = '';
7
-
8
- // If the value is a string, wrap it in quotes
9
- if (typeof value === 'string') {
10
- condition = `${key} = '${value}'`;
11
- }
12
- // If the value is a number or boolean, no quotes are needed
13
- else if (typeof value === 'number' || typeof value === 'boolean') {
14
- condition = `${key} = ${value}`;
15
- }
16
- // If the value is an array, assume it represents an IN condition
17
- else if (Array.isArray(value)) {
18
- const valueList = value.map(val =>
19
- typeof val === 'string' ? `'${val}'` : val
20
- ).join(', ');
21
- condition = `${key} IN (${valueList})`;
22
- }
23
- // If the value is an object, we can assume it's a nested condition (e.g., for complex queries)
24
- else if (typeof value === 'object') {
25
- const subConditions = Object.entries(value).map(([subKey, subValue]) => {
26
- if (typeof subValue === 'string') {
27
- return `${subKey} = '${subValue}'`;
28
- } else {
29
- return `${subKey} = ${subValue}`;
30
- }
31
- }).join(' AND ');
32
- condition = `${key} (${subConditions})`;
33
- }
34
-
35
- conditions.push(condition);
36
- }
37
-
38
- // Join the conditions with 'AND'
39
- return conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
40
- }
41
-
42
- // Example Usage
43
- const json1 = {
44
- name: "John",
45
- age: 30,
46
- active: true,
47
- hobbies: ["reading", "gaming"],
48
- address: {
49
- city: "New York",
50
- zip: "10001"
51
- }
52
- };
53
-
54
- const query1 = jsonToSqlFilter(json1);
55
- console.log(query1);
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
- type QueryCondition = {
64
- [key: string]: any; // key is field, value is condition or value
65
- };
66
-
67
- function jsonToSqlQuery(json: QueryCondition): string {
68
- const conditions: string[] = [];
69
-
70
- // Loop through each key-value pair in the JSON object
71
- for (const [key, value] of Object.entries(json)) {
72
- let condition = '';
73
-
74
- // If the value is an object (operator condition)
75
- if (typeof value === 'object' && value !== null) {
76
- const operatorConditions: string[] = [];
77
- let [operator, operatorValue]: any[] = [null, null];
78
- for ([operator, operatorValue] of Object.entries(value)) {
79
- switch (operator) {
80
- case "$eq":
81
- operatorConditions.push(`${key} = '${operatorValue}'`);
82
- break;
83
- case "$gt":
84
- operatorConditions.push(`${key} > ${operatorValue}`);
85
- break;
86
- case "$lt":
87
- operatorConditions.push(`${key} < ${operatorValue}`);
88
- break;
89
- case "$gte":
90
- operatorConditions.push(`${key} >= ${operatorValue}`);
91
- break;
92
- case "$lte":
93
- operatorConditions.push(`${key} <= ${operatorValue}`);
94
- break;
95
- case "$ne":
96
- operatorConditions.push(`${key} != '${operatorValue}'`);
97
- break;
98
- case "$in":
99
- const inValues = operatorValue.map((val: any) => typeof val === 'string' ? `'${val}'` : val).join(', ');
100
- operatorConditions.push(`${key} IN (${inValues})`);
101
- break;
102
- case "$like":
103
- operatorConditions.push(`${key} LIKE '%${operatorValue}%'`);
104
- break;
105
- default:
106
- throw new Error(`Unsupported operator: ${operator}`);
107
- }
108
- }
109
- // Join all conditions for a field with AND
110
- condition = operatorConditions.join(' AND ');
111
- } else {
112
- // For simple equality condition
113
- condition = `${key} = '${value}'`;
114
- }
115
-
116
- conditions.push(condition);
117
- }
118
-
119
- // Join the conditions with AND
120
- return conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
121
- }
122
-
123
- // Example Usage
124
- const json = {
125
- "age": { "$gt": 30 },
126
- "name": { "$eq": "John" },
127
- "status": { "$in": ["active", "pending"] },
128
- "address.city": { "$eq": "New York" }
129
- };
130
-
131
- const query = jsonToSqlQuery(json);
132
- console.log(query);