@voiceflow/common 8.3.2 → 8.5.0

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.
@@ -3,6 +3,7 @@ export declare const SLOT_REGEXP: RegExp;
3
3
  export declare const SLOT_ANNOTATION_SIMPLE_REGEX: RegExp;
4
4
  export declare const IS_VARIABLE_REGEXP: RegExp;
5
5
  export declare const READABLE_VARIABLE_REGEXP: RegExp;
6
+ export declare const VARIABLE_ONLY_REGEXP: RegExp;
6
7
  export declare const VALID_CHARACTER = "a-zA-Z";
7
8
  export declare const VALID_LATIN_CHARACTER = "a-zA-Z\u00C0-\u00FF\u0100-\u017F";
8
9
  export declare const VALID_SPOKEN_CHARACTER = "a-zA-Z\u00C0-\u00FF\u0100-\u017F\u0900-\u0965\u0970-\u097F\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFF9F\u4E00-\u9FAF\u3400-\u4DBF";
@@ -1 +1 @@
1
- {"version":3,"file":"regexp.d.ts","sourceRoot":"","sources":["../../../src/constants/regexp.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,QAAO,CAAC;AAEjC,eAAO,MAAM,WAAW,QAA2C,CAAC;AAEpE,eAAO,MAAM,4BAA4B,QAAsB,CAAC;AAEhE,eAAO,MAAM,kBAAkB,QAAW,CAAC;AAE3C,eAAO,MAAM,wBAAwB,QAAkB,CAAC;AAExD,eAAO,MAAM,eAAe,WAAW,CAAC;AAGxC,eAAO,MAAM,qBAAqB,qCAA6C,CAAC;AAKhF,eAAO,MAAM,sBAAsB,6IAAqI,CAAC;AAEzK,eAAO,MAAM,sDAAsD,mJAAoC,CAAC;AAExG,eAAO,MAAM,uBAAuB,0JAAqE,CAAC;AAG1G,eAAO,MAAM,sBAAsB,8JAAmC,CAAC"}
1
+ {"version":3,"file":"regexp.d.ts","sourceRoot":"","sources":["../../../src/constants/regexp.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,QAAO,CAAC;AAEjC,eAAO,MAAM,WAAW,QAA2C,CAAC;AAEpE,eAAO,MAAM,4BAA4B,QAAsB,CAAC;AAEhE,eAAO,MAAM,kBAAkB,QAAW,CAAC;AAE3C,eAAO,MAAM,wBAAwB,QAA2C,CAAC;AAEjF,eAAO,MAAM,oBAAoB,QAAqD,CAAC;AAEvF,eAAO,MAAM,eAAe,WAAW,CAAC;AAGxC,eAAO,MAAM,qBAAqB,qCAA6C,CAAC;AAKhF,eAAO,MAAM,sBAAsB,6IAAqI,CAAC;AAEzK,eAAO,MAAM,sDAAsD,mJAAoC,CAAC;AAExG,eAAO,MAAM,uBAAuB,0JAAqE,CAAC;AAG1G,eAAO,MAAM,sBAAsB,8JAAmC,CAAC"}
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.VALID_SAMPLE_UTTERANCE = exports.VALID_SAMPLE_CHARACTERS = exports.VALID_SAMPLE_CHARACTERS_WITHOUT_CURLY_BRACES_OR_SPACES = exports.VALID_SPOKEN_CHARACTER = exports.VALID_LATIN_CHARACTER = exports.VALID_CHARACTER = exports.READABLE_VARIABLE_REGEXP = exports.IS_VARIABLE_REGEXP = exports.SLOT_ANNOTATION_SIMPLE_REGEX = exports.SLOT_REGEXP = exports.SPACE_REGEXP = void 0;
3
+ exports.VALID_SAMPLE_UTTERANCE = exports.VALID_SAMPLE_CHARACTERS = exports.VALID_SAMPLE_CHARACTERS_WITHOUT_CURLY_BRACES_OR_SPACES = exports.VALID_SPOKEN_CHARACTER = exports.VALID_LATIN_CHARACTER = exports.VALID_CHARACTER = exports.VARIABLE_ONLY_REGEXP = exports.READABLE_VARIABLE_REGEXP = exports.IS_VARIABLE_REGEXP = exports.SLOT_ANNOTATION_SIMPLE_REGEX = exports.SLOT_REGEXP = exports.SPACE_REGEXP = void 0;
4
4
  exports.SPACE_REGEXP = / /g;
5
5
  exports.SLOT_REGEXP = /{{\[([^ .[\]{}]*?)]\.([^ .[\]{}]*?)}}/g;
6
6
  exports.SLOT_ANNOTATION_SIMPLE_REGEX = /{([^ .[\]{}]+?)}/g;
7
7
  exports.IS_VARIABLE_REGEXP = /^{.*}$/;
8
- exports.READABLE_VARIABLE_REGEXP = /{(\w{1,64})}/g;
8
+ exports.READABLE_VARIABLE_REGEXP = /{(\w{1,64}((\[\d+])*(\.\w{1,64})*)*)}/g;
9
+ exports.VARIABLE_ONLY_REGEXP = new RegExp(`^${exports.READABLE_VARIABLE_REGEXP.source}$`);
9
10
  exports.VALID_CHARACTER = 'a-zA-Z';
10
11
  // Latin character Unicode points from https://tiny.amazon.com/1hix1tfzz/enwikiorgwikiList
11
12
  exports.VALID_LATIN_CHARACTER = `${exports.VALID_CHARACTER}\xC0-\xFF\u0100-\u017F`;
@@ -1,7 +1,16 @@
1
1
  export declare const variableReplacer: (match: string, inner: string, variables: Record<string, unknown>, modifier?: (variable: unknown) => unknown) => unknown;
2
- export declare const replaceVariables: (phrase: string | undefined | null, variables: Record<string, unknown>, modifier?: ((variable: unknown) => unknown) | undefined, { trim }?: {
2
+ export declare const splitVariableName: (inner: string) => {
3
+ id: string;
4
+ path: string;
5
+ };
6
+ export declare function replaceVariables(phrase: string | undefined | null, variables: Record<string, unknown>, modifier?: (variable: unknown) => unknown, options?: {
3
7
  trim?: boolean;
4
- }) => string;
8
+ keepTypeIfOnlyVariable?: false;
9
+ }): string;
10
+ export declare function replaceVariables(phrase: string | undefined | null, variables: Record<string, unknown>, modifier?: (variable: unknown) => unknown, options?: {
11
+ trim?: boolean;
12
+ keepTypeIfOnlyVariable: true;
13
+ }): unknown;
5
14
  export declare const sanitizeVariables: (variables: Record<string, unknown>) => Record<string, unknown>;
6
15
  export declare const transformStringVariableToNumber: (str: string | number | null) => number | string | null;
7
16
  export declare const deepVariableSubstitution: <T>(bodyData: T, variableMap: Record<string, unknown>) => T;
@@ -1 +1 @@
1
- {"version":3,"file":"variables.d.ts","sourceRoot":"","sources":["../../../src/utils/variables.ts"],"names":[],"m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AAC,EAAE,KAAK,CAAA;CAAE,GAC3D,MAAM,CAAC;AACV,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,EACjC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAClC,QAAQ,CAAC,EAAE,CAAC,QAAQ,EAAE,OAAO,KAAK,OAAO,EACzC,OAAO,CAAC,EAAE;IAAE,IAAI,CAAC,EAAE,OAAO,CAAC;IAAC,sBAAsB,EAAE,IAAI,CAAA;CAAE,GACzD,OAAO,CAAC;AAuBX,eAAO,MAAM,iBAAiB,cAAe,OAAO,MAAM,EAAE,OAAO,CAAC,KAAG,OAAO,MAAM,EAAE,OAAO,CASrF,CAAC;AAET,eAAO,MAAM,+BAA+B,QAAS,MAAM,GAAG,MAAM,GAAG,IAAI,KAAG,MAAM,GAAG,MAAM,GAAG,IAY/F,CAAC;AAEF,eAAO,MAAM,wBAAwB,gBAAiB,CAAC,eAAe,OAAO,MAAM,EAAE,OAAO,CAAC,KAAG,CA2B/F,CAAC"}
@@ -1,20 +1,72 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.deepVariableSubstitution = exports.transformStringVariableToNumber = exports.sanitizeVariables = exports.replaceVariables = exports.variableReplacer = void 0;
6
+ exports.deepVariableSubstitution = exports.transformStringVariableToNumber = exports.sanitizeVariables = exports.replaceVariables = exports.splitVariableName = exports.variableReplacer = void 0;
4
7
  const constants_1 = require("../constants");
8
+ const get_js_1 = __importDefault(require("lodash/get.js"));
5
9
  const variableReplacer = (match, inner, variables, modifier) => {
6
- if (inner in variables) {
7
- return typeof modifier === 'function' ? modifier(variables[inner]) : variables[inner];
10
+ const { id, path } = (0, exports.splitVariableName)(inner);
11
+ if (!(id in variables)) {
12
+ return match;
13
+ }
14
+ if (!path) {
15
+ return typeof modifier === 'function' ? modifier(variables[id]) : variables[id];
16
+ }
17
+ try {
18
+ const variable = typeof variables[id] === 'string' ? JSON.parse(variables[id]) : variables[id];
19
+ return typeof modifier === 'function' ? modifier((0, get_js_1.default)(variable, path, 0)) : (0, get_js_1.default)(variable, path, 0);
20
+ }
21
+ catch (err) {
22
+ if (err?.message.includes('is not valid JSON')) {
23
+ return 0;
24
+ }
25
+ throw err;
8
26
  }
9
- return match;
10
27
  };
11
28
  exports.variableReplacer = variableReplacer;
12
- const replaceVariables = (phrase, variables, modifier = undefined, { trim = true } = {}) => {
29
+ const splitVariableName = (inner) => {
30
+ const firstDotIndex = inner.indexOf('.');
31
+ const firstSquareBracketIndex = inner.indexOf('[');
32
+ if (firstDotIndex === -1 && firstSquareBracketIndex === -1) {
33
+ return { id: inner, path: '' };
34
+ }
35
+ if (firstDotIndex !== -1 && firstSquareBracketIndex === -1) {
36
+ return {
37
+ id: inner.slice(0, firstDotIndex),
38
+ path: inner.slice(firstDotIndex + 1), // skip the dot
39
+ };
40
+ }
41
+ if (firstDotIndex === -1 && firstSquareBracketIndex !== -1) {
42
+ return {
43
+ id: inner.slice(0, firstSquareBracketIndex),
44
+ path: inner.slice(firstSquareBracketIndex),
45
+ };
46
+ }
47
+ if (firstDotIndex < firstSquareBracketIndex) {
48
+ return {
49
+ id: inner.slice(0, firstDotIndex),
50
+ path: inner.slice(firstDotIndex + 1), // skip the dot
51
+ };
52
+ }
53
+ return {
54
+ id: inner.slice(0, firstSquareBracketIndex),
55
+ path: inner.slice(firstSquareBracketIndex),
56
+ };
57
+ };
58
+ exports.splitVariableName = splitVariableName;
59
+ function replaceVariables(phrase, variables, modifier = undefined, { trim = true, keepTypeIfOnlyVariable = false } = {}) {
13
60
  if (!phrase || (trim && !phrase.trim())) {
14
61
  return '';
15
62
  }
63
+ if (keepTypeIfOnlyVariable && phrase.match(constants_1.VARIABLE_ONLY_REGEXP)) {
64
+ // remove the curly braces {} from phrase to get the inner
65
+ const inner = phrase.slice(1, -1);
66
+ return (0, exports.variableReplacer)(phrase, inner, variables, modifier);
67
+ }
16
68
  return phrase.replace(constants_1.READABLE_VARIABLE_REGEXP, (match, inner) => String((0, exports.variableReplacer)(match, inner, variables, modifier)));
17
- };
69
+ }
18
70
  exports.replaceVariables = replaceVariables;
19
71
  // turn float variables to 4 decimal places
20
72
  const sanitizeVariables = (variables) => Object.entries(variables).reduce((acc, [key, value]) => {
@@ -44,7 +96,7 @@ const deepVariableSubstitution = (bodyData, variableMap) => {
44
96
  return subCollection;
45
97
  }
46
98
  if (typeof subCollection === 'string') {
47
- return (0, exports.replaceVariables)(subCollection, variableMap, modifier);
99
+ return replaceVariables(subCollection, variableMap, modifier);
48
100
  }
49
101
  if (Array.isArray(subCollection)) {
50
102
  return subCollection.map((v) => _recurse(v, modifier));
@@ -3,6 +3,7 @@ export declare const SLOT_REGEXP: RegExp;
3
3
  export declare const SLOT_ANNOTATION_SIMPLE_REGEX: RegExp;
4
4
  export declare const IS_VARIABLE_REGEXP: RegExp;
5
5
  export declare const READABLE_VARIABLE_REGEXP: RegExp;
6
+ export declare const VARIABLE_ONLY_REGEXP: RegExp;
6
7
  export declare const VALID_CHARACTER = "a-zA-Z";
7
8
  export declare const VALID_LATIN_CHARACTER = "a-zA-Z\u00C0-\u00FF\u0100-\u017F";
8
9
  export declare const VALID_SPOKEN_CHARACTER = "a-zA-Z\u00C0-\u00FF\u0100-\u017F\u0900-\u0965\u0970-\u097F\u3000-\u303F\u3040-\u309F\u30A0-\u30FF\uFF00-\uFF9F\u4E00-\u9FAF\u3400-\u4DBF";
@@ -1 +1 @@
1
- {"version":3,"file":"regexp.d.ts","sourceRoot":"","sources":["../../../src/constants/regexp.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,QAAO,CAAC;AAEjC,eAAO,MAAM,WAAW,QAA2C,CAAC;AAEpE,eAAO,MAAM,4BAA4B,QAAsB,CAAC;AAEhE,eAAO,MAAM,kBAAkB,QAAW,CAAC;AAE3C,eAAO,MAAM,wBAAwB,QAAkB,CAAC;AAExD,eAAO,MAAM,eAAe,WAAW,CAAC;AAGxC,eAAO,MAAM,qBAAqB,qCAA6C,CAAC;AAKhF,eAAO,MAAM,sBAAsB,6IAAqI,CAAC;AAEzK,eAAO,MAAM,sDAAsD,mJAAoC,CAAC;AAExG,eAAO,MAAM,uBAAuB,0JAAqE,CAAC;AAG1G,eAAO,MAAM,sBAAsB,8JAAmC,CAAC"}
1
+ {"version":3,"file":"regexp.d.ts","sourceRoot":"","sources":["../../../src/constants/regexp.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,QAAO,CAAC;AAEjC,eAAO,MAAM,WAAW,QAA2C,CAAC;AAEpE,eAAO,MAAM,4BAA4B,QAAsB,CAAC;AAEhE,eAAO,MAAM,kBAAkB,QAAW,CAAC;AAE3C,eAAO,MAAM,wBAAwB,QAA2C,CAAC;AAEjF,eAAO,MAAM,oBAAoB,QAAqD,CAAC;AAEvF,eAAO,MAAM,eAAe,WAAW,CAAC;AAGxC,eAAO,MAAM,qBAAqB,qCAA6C,CAAC;AAKhF,eAAO,MAAM,sBAAsB,6IAAqI,CAAC;AAEzK,eAAO,MAAM,sDAAsD,mJAAoC,CAAC;AAExG,eAAO,MAAM,uBAAuB,0JAAqE,CAAC;AAG1G,eAAO,MAAM,sBAAsB,8JAAmC,CAAC"}
@@ -2,7 +2,8 @@ export const SPACE_REGEXP = / /g;
2
2
  export const SLOT_REGEXP = /{{\[([^ .[\]{}]*?)]\.([^ .[\]{}]*?)}}/g;
3
3
  export const SLOT_ANNOTATION_SIMPLE_REGEX = /{([^ .[\]{}]+?)}/g;
4
4
  export const IS_VARIABLE_REGEXP = /^{.*}$/;
5
- export const READABLE_VARIABLE_REGEXP = /{(\w{1,64})}/g;
5
+ export const READABLE_VARIABLE_REGEXP = /{(\w{1,64}((\[\d+])*(\.\w{1,64})*)*)}/g;
6
+ export const VARIABLE_ONLY_REGEXP = new RegExp(`^${READABLE_VARIABLE_REGEXP.source}$`);
6
7
  export const VALID_CHARACTER = 'a-zA-Z';
7
8
  // Latin character Unicode points from https://tiny.amazon.com/1hix1tfzz/enwikiorgwikiList
8
9
  export const VALID_LATIN_CHARACTER = `${VALID_CHARACTER}\xC0-\xFF\u0100-\u017F`;
@@ -1,7 +1,16 @@
1
1
  export declare const variableReplacer: (match: string, inner: string, variables: Record<string, unknown>, modifier?: (variable: unknown) => unknown) => unknown;
2
- export declare const replaceVariables: (phrase: string | undefined | null, variables: Record<string, unknown>, modifier?: ((variable: unknown) => unknown) | undefined, { trim }?: {
2
+ export declare const splitVariableName: (inner: string) => {
3
+ id: string;
4
+ path: string;
5
+ };
6
+ export declare function replaceVariables(phrase: string | undefined | null, variables: Record<string, unknown>, modifier?: (variable: unknown) => unknown, options?: {
3
7
  trim?: boolean;
4
- }) => string;
8
+ keepTypeIfOnlyVariable?: false;
9
+ }): string;
10
+ export declare function replaceVariables(phrase: string | undefined | null, variables: Record<string, unknown>, modifier?: (variable: unknown) => unknown, options?: {
11
+ trim?: boolean;
12
+ keepTypeIfOnlyVariable: true;
13
+ }): unknown;
5
14
  export declare const sanitizeVariables: (variables: Record<string, unknown>) => Record<string, unknown>;
6
15
  export declare const transformStringVariableToNumber: (str: string | number | null) => number | string | null;
7
16
  export declare const deepVariableSubstitution: <T>(bodyData: T, variableMap: Record<string, unknown>) => T;
@@ -1 +1 @@
1
- {"version":3,"file":"variables.d.ts","sourceRoot":"","sources":["../../../src/utils/variables.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,gBAAgB,UACpB,MAAM,SACN,MAAM,aACF,OAAO,MAAM,EAAE,OAAO,CAAC,aACvB,CAAC,QAAQ,EAAE,OAAO,KAAK,OAAO,KACxC,OAMF,CAAC;AAEF,eAAO,MAAM,gBAAgB,WACnB,MAAM,GAAG,SAAS,GAAG,IAAI,aACtB,OAAO,MAAM,EAAE,OAAO,CAAC,aACxB,CAAC,CAAC,QAAQ,EAAE,OAAO,KAAK,OAAO,CAAC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BAAsB,EAAE,IAAI,CAAA;CAAE,GACzD,OAAO,CAAC;AAuBX,eAAO,MAAM,iBAAiB,cAAe,OAAO,MAAM,EAAE,OAAO,CAAC,KAAG,OAAO,MAAM,EAAE,OAAO,CASrF,CAAC;AAET,eAAO,MAAM,+BAA+B,QAAS,MAAM,GAAG,MAAM,GAAG,IAAI,KAAG,MAAM,GAAG,MAAM,GAAG,IAY/F,CAAC;AAEF,eAAO,MAAM,wBAAwB,gBAAiB,CAAC,eAAe,OAAO,MAAM,EAAE,OAAO,CAAC,KAAG,CA2B/F,CAAC"}
@@ -1,16 +1,64 @@
1
- import { READABLE_VARIABLE_REGEXP } from '../constants/index.js';
1
+ import { READABLE_VARIABLE_REGEXP, VARIABLE_ONLY_REGEXP } from '../constants/index.js';
2
+ import _get from 'lodash/get.js';
2
3
  export const variableReplacer = (match, inner, variables, modifier) => {
3
- if (inner in variables) {
4
- return typeof modifier === 'function' ? modifier(variables[inner]) : variables[inner];
4
+ const { id, path } = splitVariableName(inner);
5
+ if (!(id in variables)) {
6
+ return match;
5
7
  }
6
- return match;
8
+ if (!path) {
9
+ return typeof modifier === 'function' ? modifier(variables[id]) : variables[id];
10
+ }
11
+ try {
12
+ const variable = typeof variables[id] === 'string' ? JSON.parse(variables[id]) : variables[id];
13
+ return typeof modifier === 'function' ? modifier(_get(variable, path, 0)) : _get(variable, path, 0);
14
+ }
15
+ catch (err) {
16
+ if (err?.message.includes('is not valid JSON')) {
17
+ return 0;
18
+ }
19
+ throw err;
20
+ }
21
+ };
22
+ export const splitVariableName = (inner) => {
23
+ const firstDotIndex = inner.indexOf('.');
24
+ const firstSquareBracketIndex = inner.indexOf('[');
25
+ if (firstDotIndex === -1 && firstSquareBracketIndex === -1) {
26
+ return { id: inner, path: '' };
27
+ }
28
+ if (firstDotIndex !== -1 && firstSquareBracketIndex === -1) {
29
+ return {
30
+ id: inner.slice(0, firstDotIndex),
31
+ path: inner.slice(firstDotIndex + 1), // skip the dot
32
+ };
33
+ }
34
+ if (firstDotIndex === -1 && firstSquareBracketIndex !== -1) {
35
+ return {
36
+ id: inner.slice(0, firstSquareBracketIndex),
37
+ path: inner.slice(firstSquareBracketIndex),
38
+ };
39
+ }
40
+ if (firstDotIndex < firstSquareBracketIndex) {
41
+ return {
42
+ id: inner.slice(0, firstDotIndex),
43
+ path: inner.slice(firstDotIndex + 1), // skip the dot
44
+ };
45
+ }
46
+ return {
47
+ id: inner.slice(0, firstSquareBracketIndex),
48
+ path: inner.slice(firstSquareBracketIndex),
49
+ };
7
50
  };
8
- export const replaceVariables = (phrase, variables, modifier = undefined, { trim = true } = {}) => {
51
+ export function replaceVariables(phrase, variables, modifier = undefined, { trim = true, keepTypeIfOnlyVariable = false } = {}) {
9
52
  if (!phrase || (trim && !phrase.trim())) {
10
53
  return '';
11
54
  }
55
+ if (keepTypeIfOnlyVariable && phrase.match(VARIABLE_ONLY_REGEXP)) {
56
+ // remove the curly braces {} from phrase to get the inner
57
+ const inner = phrase.slice(1, -1);
58
+ return variableReplacer(phrase, inner, variables, modifier);
59
+ }
12
60
  return phrase.replace(READABLE_VARIABLE_REGEXP, (match, inner) => String(variableReplacer(match, inner, variables, modifier)));
13
- };
61
+ }
14
62
  // turn float variables to 4 decimal places
15
63
  export const sanitizeVariables = (variables) => Object.entries(variables).reduce((acc, [key, value]) => {
16
64
  if (typeof value === 'number' && !Number.isInteger(value)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voiceflow/common",
3
- "version": "8.3.2",
3
+ "version": "8.5.0",
4
4
  "description": "Junk drawer of utility functions",
5
5
  "keywords": [
6
6
  "voiceflow"
@@ -60,5 +60,5 @@
60
60
  "publishConfig": {
61
61
  "access": "public"
62
62
  },
63
- "gitHead": "eb8d92af87448d5f34d648d0e1e48992c42ec289"
63
+ "gitHead": "df18abdc19c00ce85d2ded6f1d07d879c3b6d2b5"
64
64
  }