UNPKG

n8n-nodes-israeli-phone

Version:

n8n node for formatting Israeli phone numbers into all possible variants

105 lines 3.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IsraeliPhone = void 0; const n8n_workflow_1 = require("n8n-workflow"); class IsraeliPhone { constructor() { this.description = { displayName: 'Israeli Phone', name: 'israeliPhone', icon: 'file:israeliphone.svg', group: ['transform'], version: 1, description: 'Formats Israeli phone numbers into all possible variants', defaults: { name: 'Israeli Phone', }, inputs: ["main"], outputs: ["main"], usableAsTool: true, properties: [ { displayName: 'Phone Number', name: 'phoneNumber', type: 'string', default: '', placeholder: '050-123-4567', description: 'The Israeli phone number to format', required: true, }, ], }; } static normalizeAllVariants(phone) { if (!phone) return {}; let cleaned = phone.replace(/\D/g, ''); let localNumber = cleaned; if (cleaned.startsWith('00')) { cleaned = cleaned.slice(2); } if (cleaned.startsWith('972')) { localNumber = '0' + cleaned.slice(3); } else if (cleaned.length === 10 && cleaned.startsWith('05')) { localNumber = cleaned; } if (!/^05\d{8}$/.test(localNumber)) { return {}; } const prefix = localNumber.slice(0, 3); const middle = localNumber.slice(3, 6); const end = localNumber.slice(6); return { original: phone, cleanedDigits: cleaned, local: localNumber, withPlus972: '+972' + localNumber.slice(1), with972: '972' + localNumber.slice(1), dashed: `${prefix}-${middle}-${end}`, spaced: `${prefix} ${middle} ${end}`, dotted: `${prefix}.${middle}.${end}`, internationalDashed: `+972-${prefix.slice(1)}-${middle}-${end}`, internationalSpaced: `+972 ${prefix.slice(1)} ${middle} ${end}`, }; } async execute() { const items = this.getInputData(); const returnData = []; for (let itemIndex = 0; itemIndex < items.length; itemIndex++) { try { const phoneNumber = this.getNodeParameter('phoneNumber', itemIndex, ''); const item = items[itemIndex]; const variants = IsraeliPhone.normalizeAllVariants(phoneNumber); returnData.push({ json: { ...item.json, ...variants, }, pairedItem: itemIndex, }); } catch (error) { if (this.continueOnFail()) { returnData.push({ json: this.getInputData(itemIndex)[0].json, error, pairedItem: itemIndex, }); } else { if (error.context) { error.context.itemIndex = itemIndex; throw error; } throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex, }); } } } return [returnData]; } } exports.IsraeliPhone = IsraeliPhone; //# sourceMappingURL=IsraeliPhone.node.js.map