New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

proto-expand

Package Overview
Dependencies
Maintainers
0
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

proto-expand - npm Package Compare versions

Comparing version 1.1.7 to 1.1.10

99

dist/index.d.ts

@@ -0,1 +1,18 @@

/**
* * A contrived function to convert the text inside the 'proto' to an array of string arrays
* 1. First create a well-formed JSON
* 2. Convert to an `Array<Array<string>>`
* @param str
*/
declare const parseProtoStringToArray: (str: string) => string[][];
/**
* Another contrived function
* Get a Single quoted JSON (As the ones coming in the 'proto') and parse it
* To do that:
* 1. quotes not preceded by backslash are replaced by double quotes
* 2. the ones preceded by backslash are replaced by a single quote
* @param str
*/
declare const parseSingleQuotedJson: (str: string) => any;
type Value = number | string | null | boolean;

@@ -12,3 +29,3 @@ type FlatPayload = {

/**
* ## Given a payload extract a summary of array properties and their `Dimensions`
* ## Given a payload.json extract a summary of array properties and their `Dimensions`
*

@@ -37,4 +54,4 @@ * @example

* ```ts
* flattenPayload({a: 10, b: [{c: 10}, {c: 20}] })
* result = {a: 10, c_1: 10, c_2: 20}
* flattenPayload({a: 10, b: door: [{c: 10}, {c: 20}] })
* result = {a: 10, c_door_1: 10, c_door_2: 20}
* ```

@@ -96,39 +113,43 @@ */

/**
* Convert an array of activities to a string using the strange 'proto' format
* A class representing a SWAR **ProcessDefinition** (Also called `Protocol`)
* It contains an array of `Activity`
* @see Activity
*/
declare const exportToString: (proto: Array<Activity>) => string;
/**
* * A contrived function to convert the text inside the 'proto' to an array of string arrays
* 1. First create a well-formed JSON
* 2. Convert to an `Array<Array<string>>`
* @param str
*/
declare const parseProtoStringToArray: (str: string) => string[][];
/**
* Another contrived function
* Get a Single quoted JSON (As the ones coming in the 'proto') and parse it
* To do that:
* 1. quotes not preceded by backslash are replaced by double quotes
* 2. the ones preceded by backslash are replaced by a single quote
* @param str
*/
declare const parseSingleQuotedJson: (str: string) => any;
/**
* From an string to an array of activities
*/
declare const parseProtoString: (str: string) => Activity[];
/**
* Given an array of activities expands the variables inside them according to the array dimensions
* A new `Array<Activity>` is returned
* @param activities
* @param dimensions
* @returns the expanded activities
*/
declare const expandAllActivities: (activities: Array<Activity>, dimensions: Dimensions) => Activity[];
/**
* Parse a proto array of instructions creating an array of activities.
* Because we need to group instructions with the same activity we need a somewhat complex logic
*/
declare const parseProtoArray: (proto: Array<Array<string>>) => Array<Activity>;
declare class ProcessDefinition {
readonly activities: Array<Activity>;
constructor(activities: Array<Activity>);
/**
* Contrived method that gets the activities from the process and returns an `string` in the "proto" format
*/
exportToProtoString(): string;
/**
* Expand the variables inside the process according to the dimensions array
* A new `Process` is returned
* @param dimensions
* @returns the new expanded process
*/
expand(dimensions: Dimensions): ProcessDefinition;
/**
* Do the whole expansion from a proto string back to an expanded proto string
* @param proto
* @param dimensions
* @returns the new expanded proto string
*/
static expandProto(proto: string, dimensions: Dimensions): string;
/**
* Creates a ProcessDefinition from a given proto array.
*
* @param {string[][]} proto - The proto array to parse.
* @return {ProcessDefinition} The parsed ProcessDefinition.
*/
static fromProtoArray(proto: string[][]): ProcessDefinition;
/**
* Creates a ProcessDefinition from a given proto string.
*
* @param {string} proto - The proto string to parse.
* @return {ProcessDefinition} The parsed ProcessDefinition.
*/
static parseProtoString(proto: string): ProcessDefinition;
}
export { type Dimensions, type FlatPayload, type Payload, type Value, createAssignment, expandAllActivities, exportToString, extractDimensions, flattenPayload, parseProtoArray, parseProtoString, parseProtoStringToArray, parseSingleQuotedJson, signature };
export { type Dimensions, type FlatPayload, type Payload, ProcessDefinition, type Value, createAssignment, extractDimensions, flattenPayload, parseProtoStringToArray, parseSingleQuotedJson, signature };

@@ -36,9 +36,6 @@ var __defProp = Object.defineProperty;

__export(src_exports, {
ProcessDefinition: () => ProcessDefinition,
createAssignment: () => createAssignment,
expandAllActivities: () => expandAllActivities,
exportToString: () => exportToString,
extractDimensions: () => extractDimensions,
flattenPayload: () => flattenPayload,
parseProtoArray: () => parseProtoArray,
parseProtoString: () => parseProtoString,
parseProtoStringToArray: () => parseProtoStringToArray,

@@ -50,2 +47,82 @@ parseSingleQuotedJson: () => parseSingleQuotedJson,

// src/parse.ts
var parseProtoStringToArray = (str) => {
const cleanText = "[" + // Add first bracket
str.replace(/--[^\n]*\n/, "").replace(/\\'/g, "\\\\'") + "[] ]";
return JSON.parse(cleanText).slice(0, -1);
};
var parseSingleQuotedJson = (str) => {
let cleanText = str.replace(new RegExp("(?<!\\\\)'", "g"), '"').replace(/\\'/g, "'");
return JSON.parse(cleanText);
};
// src/payload.ts
var extractDimensions = (payload) => {
const result = {};
for (const key in payload) {
const value = payload[key];
if (Array.isArray(value)) result[key] = value.length;
}
return result;
};
var signature = (dimensions) => Object.entries(dimensions).sort().map(([_, v]) => v).reduce((r, v) => r + "@" + v, "");
var flattenPayload = (payload) => {
const result = {};
for (const v_key in payload) {
const value = payload[v_key];
if (Array.isArray(value)) {
value.forEach((v, i) => {
for (const key in v) result[key + "_" + v_key + "_" + (i + 1)] = v[key];
});
} else {
result[v_key] = value;
}
}
return result;
};
var createAssignment = (protocol, payload, username, when = /* @__PURE__ */ new Date()) => {
let fp = flattenPayload(payload);
const expandedValues = {};
for (const key in fp) {
expandedValues[key] = { Observed: fp[key] };
}
let dimensions = extractDimensions(payload);
return {
Who: [{ username }],
When: when.getTime(),
Status: __spreadValues({
Protocolo: {
Observed: protocol + signature(dimensions),
Version: 1.2
}
}, expandedValues),
Dimensions: dimensions
};
};
// src/activity.ts
var escapeQuotes = (str) => str.replace(/'/g, "\\'");
var Activity = class _Activity {
constructor(id, description, instructions = []) {
this.id = id;
this.description = description;
for (const instruction of instructions) instruction.activity = this;
this.instructions = instructions;
}
expandInstructions(counts) {
return new _Activity(
this.id,
this.description,
this.instructions.flatMap((instruction) => instruction.expand(counts))
);
}
toString(seq = 1) {
return `{'Activity_Id':'${escapeQuotes(this.id)}','Activity_Dsc':'${escapeQuotes(this.description)}','Activity_Seq':'${seq}'}`;
}
static fromString(str) {
const result = parseSingleQuotedJson(str);
return new _Activity(result.Activity_Id, result.Activity_Dsc);
}
};
// src/instruction.ts

@@ -61,3 +138,3 @@ var Instruction = class _Instruction {

toString(activitySeq = 1, step = 1) {
return '[\n "' + this.toArray(activitySeq, step).join('",\n "') + '"],';
return '[\n "' + this.toArray(activitySeq, step).map((s) => s.replace(/"/g, '\\"')).join('",\n "') + '"],';
}

@@ -68,6 +145,6 @@ expand(dimensions) {

return [this];
const [regexp, n] = v;
const [regexp, n, name] = v;
const instructions = new Array();
for (let i = 1; i <= n; i++) {
let row = this.values.map((str) => expand(regexp, i, str));
let row = this.values.map((str) => expand(regexp, i, name, str));
instructions.push(new _Instruction(this.activity, row));

@@ -81,8 +158,9 @@ }

const v = findVariable(counts, str);
if (v)
return v;
if (v) return v;
}
return null;
};
var expand = (id, value, str) => str.replace(id, String(value));
var expand = (id, value, name, str) => {
return str.replace(id, name + "_" + String(value));
};
var REGEXP_ID = /@[a-zA-Z_]\w*/g;

@@ -98,44 +176,58 @@ var findVariable = (counts, str) => {

throw new Error(`Invalid array value: '${id}'`);
return [new RegExp("@" + id, "g"), count];
return [new RegExp("@" + id, "g"), count, id];
};
// src/activity.ts
var escapeQuotes = (str) => str.replace(/'/g, "\\'");
var Activity = class _Activity {
constructor(id, description, instructions = []) {
this.id = id;
this.description = description;
for (const instruction of instructions)
instruction.activity = this;
this.instructions = instructions;
// src/processDefinition.ts
var configurationActivityId = "configuracionProtocolo";
var ProcessDefinition = class _ProcessDefinition {
//configurationData: {[key: string]: any};
constructor(activities) {
this.activities = activities;
}
expandInstructions(counts) {
return new _Activity(
this.id,
this.description,
this.instructions.flatMap((instruction) => instruction.expand(counts))
);
/**
* Contrived method that gets the activities from the process and returns an `string` in the "proto" format
*/
exportToProtoString() {
return this.activities.flatMap((activity, activityIndex) => activity.instructions.map((instruction, instructionIndex) => {
let activitySeq = activity.id == configurationActivityId || activity.id == "InitialForm" ? 0 : activityIndex - 1;
return instruction.toString(activitySeq, instructionIndex + 1);
})).join("\n") + "\n";
}
toString(seq = 1) {
return `{'Activity_Id':'${escapeQuotes(this.id)}','Activity_Dsc':'${escapeQuotes(this.description)}','Activity_Seq':'${seq}'}`;
/**
* Expand the variables inside the process according to the dimensions array
* A new `Process` is returned
* @param dimensions
* @returns the new expanded process
*/
expand(dimensions) {
return new _ProcessDefinition(this.activities.map((activity) => activity.expandInstructions(dimensions)));
}
static fromString(str) {
const result = parseSingleQuotedJson(str);
return new _Activity(result.Activity_Id, result.Activity_Dsc);
/**
* Do the whole expansion from a proto string back to an expanded proto string
* @param proto
* @param dimensions
* @returns the new expanded proto string
*/
static expandProto(proto, dimensions) {
return _ProcessDefinition.parseProtoString(proto).expand(dimensions).exportToProtoString();
}
/**
* Creates a ProcessDefinition from a given proto array.
*
* @param {string[][]} proto - The proto array to parse.
* @return {ProcessDefinition} The parsed ProcessDefinition.
*/
static fromProtoArray(proto) {
return new _ProcessDefinition(parseProtoArray(proto));
}
/**
* Creates a ProcessDefinition from a given proto string.
*
* @param {string} proto - The proto string to parse.
* @return {ProcessDefinition} The parsed ProcessDefinition.
*/
static parseProtoString(proto) {
return _ProcessDefinition.fromProtoArray(parseProtoStringToArray(proto));
}
};
// src/parse.ts
var exportToString = (proto) => proto.flatMap((activity, activityIndex) => activity.instructions.map((instruction, instructionIndex) => instruction.toString(activityIndex + 1, instructionIndex + 1))).join("\n") + "\n";
var parseProtoStringToArray = (str) => {
const cleanText = "[" + // Add first bracket
str.replace(/--[^\n]*\n/, "").replace(/\\'/g, "\\\\'") + "[] ]";
return JSON.parse(cleanText).slice(0, -1);
};
var parseSingleQuotedJson = (str) => {
let cleanText = str.replace(new RegExp("(?<!\\\\)'", "g"), '"').replace(/\\'/g, "'");
return JSON.parse(cleanText);
};
var parseProtoString = (str) => parseProtoArray(parseProtoStringToArray(str));
var expandAllActivities = (activities, dimensions) => activities.map((activity) => activity.expandInstructions(dimensions));
var parseProtoArray = (proto) => {

@@ -146,4 +238,3 @@ const result = [];

const addActivity = () => {
if (current)
result.push(new Activity(current.id, current.description, instructions));
if (current) result.push(new Activity(current.id, current.description, instructions));
};

@@ -164,57 +255,8 @@ proto.forEach((row) => {

};
// src/payload.ts
var extractDimensions = (payload) => {
const result = {};
for (const key in payload) {
const value = payload[key];
if (Array.isArray(value))
result[key] = value.length;
}
return result;
};
var signature = (dimensions) => Object.entries(dimensions).sort().map(([_, v]) => v).reduce((r, v) => r + "@" + v, "");
var flattenPayload = (payload) => {
const result = {};
for (const key in payload) {
const value = payload[key];
if (Array.isArray(value)) {
value.forEach((v, i) => {
for (const key2 in v)
result[key2 + "_" + (i + 1)] = v[key2];
});
} else {
result[key] = value;
}
}
return result;
};
var createAssignment = (protocol, payload, username, when = /* @__PURE__ */ new Date()) => {
let fp = flattenPayload(payload);
const expandedValues = {};
for (const key in fp) {
expandedValues[key] = { Observed: fp[key] };
}
let dimensions = extractDimensions(payload);
return {
Who: [{ username }],
When: when.getTime(),
Status: __spreadValues({
Protocolo: {
Observed: protocol + signature(dimensions),
Version: 1.2
}
}, expandedValues),
Dimensions: dimensions
};
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ProcessDefinition,
createAssignment,
expandAllActivities,
exportToString,
extractDimensions,
flattenPayload,
parseProtoArray,
parseProtoString,
parseProtoStringToArray,

@@ -221,0 +263,0 @@ parseSingleQuotedJson,

{
"name": "proto-expand",
"version": "1.1.7",
"version": "1.1.10",
"description": "",

@@ -5,0 +5,0 @@ "main": "./dist/index.js",

@@ -76,11 +76,15 @@ # Proto 'planilla' expander

const activities: Array<Activity> = parseProtoString("A proto file loaded in a string")
const process: Process = Process.parseProtoString("A proto file loaded in a string")
// Expand the variables
const expandedActivities: Array<Activity> = expandAllActivities(activities, dimensions)
const expandedProcess: Process = process.expand(dimensions)
// Convert the array back to a big `string`
const bigString = exportToString(expandedActivities)
const bigString = expandedProcess.exportToProtoString()
// Or in a one liner
const expandedString = Process.expandProto("A proto file loaded in a string", dimensions)
```

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc