From 84b46d02c95fd272257522dc05dff615829d0feb Mon Sep 17 00:00:00 2001 From: Mihajlo Medjedovic Date: Thu, 22 Sep 2022 15:56:48 +0200 Subject: [PATCH] chore: improved algorithm for detecting log off word --- src/utils/sas9/extractUserNameSas9.ts | 34 +++++++-- src/utils/spec/extractUserNameSas9.spec.ts | 82 ++++++++++++++++++---- 2 files changed, 97 insertions(+), 19 deletions(-) diff --git a/src/utils/sas9/extractUserNameSas9.ts b/src/utils/sas9/extractUserNameSas9.ts index 9509a4b..93f48fc 100644 --- a/src/utils/sas9/extractUserNameSas9.ts +++ b/src/utils/sas9/extractUserNameSas9.ts @@ -1,10 +1,32 @@ -export const extractUserNameSas9 = (response: string) => { - const regex = /"title":\s?"Log Off [0-1a-zA-Z ]*"/ - const fallbackRegex = /"title":\s?"[0-1a-zA-Z ]*"/ - const matched = response?.match(regex) || response?.match(fallbackRegex) - const username = matched?.[0].slice(17, -1) +/** + * Dictionary should contain only languages in SAS where `logout` text + * is represented with more then one word + */ +const dictionary = ['Log Off'] - if (!username) return 'unknown (error fetching username)' +/** + * Extracts username assuming the first word after "title" means log off if not found otherwise in the dictionary + * @param response SAS response content + * @returns username + */ +export const extractUserNameSas9 = (response: string) => { + const regex = /"title":\s?".*"/ + + const matched = response?.match(regex) + let username = matched?.[0].split(':')[1].trim() + let breakIndex = username?.indexOf(' ') + + dictionary.map((logoutWord) => { + const index = username?.indexOf(logoutWord) || -1 + + if (index > -1) { + breakIndex = index + logoutWord.length + } + }) + + username = username?.slice(breakIndex, -1) + + if (!username) return 'unknown' return username.trim() } diff --git a/src/utils/spec/extractUserNameSas9.spec.ts b/src/utils/spec/extractUserNameSas9.spec.ts index 91b4a81..7eeed79 100644 --- a/src/utils/spec/extractUserNameSas9.spec.ts +++ b/src/utils/spec/extractUserNameSas9.spec.ts @@ -1,41 +1,97 @@ import { extractUserNameSas9 } from '../sas9/extractUserNameSas9' -describe('Extract username SAS9', () => { - it('should return username', () => { - const response = ` "title": "Log Off SAS User One",` +describe('Extract username SAS9 English - two word logout handled language', () => { + const logoutWord = 'Log Off' + + it('should return username with space after colon', () => { + const response = ` "title": "${logoutWord} SAS User One",` const username = extractUserNameSas9(response) expect(username).toEqual('SAS User One') }) - it('should return username with fallback regex', () => { - const response = ` "title": "Logout SAS User One",` + it('should return username without space after colon', () => { + const response = ` "title":"${logoutWord} SAS User One",` const username = extractUserNameSas9(response) expect(username).toEqual('SAS User One') }) + it('should return username with one word user name', () => { + const response = ` "title": "${logoutWord} SasUserOne",` + const username = extractUserNameSas9(response) + + expect(username).toEqual('SasUserOne') + }) + it('should return username unknown', () => { const response = ` invalid",` const username = extractUserNameSas9(response) - expect(username).toEqual('unknown (error fetching username)') + expect(username).toEqual('unknown') + }) +}) + +describe('Extract username SAS9 two word logout unhandled language', () => { + const logoutWord = 'Log out' + + it('should return username with space after colon', () => { + const response = ` "title": "${logoutWord} SAS User One",` + const username = extractUserNameSas9(response) + + expect(username).toEqual('out SAS User One') + }) + + it('should return username without space after colon', () => { + const response = ` "title":"${logoutWord} SAS User One",` + const username = extractUserNameSas9(response) + + expect(username).toEqual('out SAS User One') }) it('should return username with one word user name', () => { - const response = ` "title": "Log Off SasUserOne",` + const response = ` "title": "${logoutWord} SasUserOne",` + const username = extractUserNameSas9(response) + + expect(username).toEqual('out SasUserOne') + }) + + it('should return username unknown', () => { + const response = ` invalid",` + const username = extractUserNameSas9(response) + + expect(username).toEqual('unknown') + }) +}) + +describe('Extract username SAS9 Spasnish - one word logout languages', () => { + const logoutWord = 'Desconexión' + + it('should return username with space after colon', () => { + const response = ` "title": "${logoutWord} SAS User One",` + const username = extractUserNameSas9(response) + + expect(username).toEqual('SAS User One') + }) + + it('should return username without space after colon', () => { + const response = ` "title":"${logoutWord} SAS User One",` + const username = extractUserNameSas9(response) + + expect(username).toEqual('SAS User One') + }) + + it('should return username with one word user name', () => { + const response = ` "title": "${logoutWord} SasUserOne",` const username = extractUserNameSas9(response) expect(username).toEqual('SasUserOne') }) - it('should return username with unhandled Spanish language', () => { - const response = ` "title": "Desconectarse SAS User One",` + it('should return username unknown', () => { + const response = ` invalid",` const username = extractUserNameSas9(response) - // Result won't be perfect but it will work Result will be: ctasasuseone - // instead of sasuseone - - expect(username).toEqual('ctarse SAS User One') + expect(username).toEqual('unknown') }) })