How to Extract a Number from a Previous Step and Format it to a String?

This topic was automatically generated from Slack. You can find the original thread here.

how do I extract a number from a previous step and format it to a string?

To extract a number from a previous step and format it as a string, you can use the following code in a new step within your Pipedream workflow: ```
export default defineComponent({ async run({ steps }) { // Assuming the previous step is named “previousStep” const number = steps.previousStep.result; // Replace “previousStep” with the actual step name const formattedString = number.toString(); return formattedString; }, });

ow do I extract a number from a previous step’s json body and format it to a string?

To extract a number from a previous step’s JSON body and format it as a string, you can use the following code in a new step within your Pipedream workflow: ```
export default defineComponent({ async run({ steps }) { // Assuming the previous step is named “previousStep” // Replace “previousStep” with the actual step name // Replace “path.to.number” with the actual path to the number in the JSON body const number = steps.previousStep.response.path.to.number; const formattedString = number.toString(); return formattedString; }, });

how do I format a string into the following NANP regex format: ((([0-9]{3}) ?)|[0-9]{3}-)[0-9]{3}-[0-9]{4}

To format a string into the NANP regex format you mentioned, you can use the following code in a Pipedream component: export default defineComponent({ props: { inputString: { type: "string", label: "Input String", }, }, async run({ steps }) { const formattedString = this.inputString.replace( /^(\d{3})(\d{3})(\d{4})$/, "($1) $2-$3" ); return formattedString; }, }); This component takes an input string (a 10-digit number) and formats it using the NANP regex format you provided. Replace inputString with the actual string you want to format.

how do I extract a number from a previous step’s json body, and format it into a with the following NANP regex format: ((([0-9]{3}) ?)|[0-9]{3}-)[0-9]{3}-[0-9]{4}

To extract a number from a previous step’s JSON body and format it into the NANP regex format, you can use the following code in a new step within your Pipedream workflow: ```
export default defineComponent({ async run({ steps }) { // Assuming the previous step is named “previousStep” // Replace “previousStep” with the actual step name // Replace “path.to.number” with the actual path to the number in the JSON body const number = steps.previousStep.response.path.to.number; // Ensure the number has 10 digits if (number.toString().length === 10) { const formattedString = number.toString().replace( /^(\d{3})(\d{3})(\d{4})$/, “($1) $2-$3” ); return formattedString; } else { throw new Error(“The extracted number is not a valid 10-digit number.”); } }, });

how do I extract a time from a previous step’s JSON body and format it into UNIX timestamp format?

To extract a time from a previous step’s JSON body and format it into a UNIX timestamp, you can use the following code in a new step within your Pipedream workflow: ```
export default defineComponent({ async run({ steps }) { // Assuming the previous step is named “previousStep” // Replace “previousStep” with the actual step name // Replace “path.to.time” with the actual path to the time in the JSON body const timeString = steps.previousStep.response.path.to.time; // Convert the time string to a Date object const dateObj = new Date(timeString); // Convert the Date object to a UNIX timestamp const unixTimestamp = Math.floor(dateObj.getTime() / 1000); return unixTimestamp; }, });