mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-16 16:40:06 +00:00
chore: improved algorithm for detecting log off word
This commit is contained in:
@@ -1,10 +1,32 @@
|
|||||||
export const extractUserNameSas9 = (response: string) => {
|
/**
|
||||||
const regex = /"title":\s?"Log Off [0-1a-zA-Z ]*"/
|
* Dictionary should contain only languages in SAS where `logout` text
|
||||||
const fallbackRegex = /"title":\s?"[0-1a-zA-Z ]*"/
|
* is represented with more then one word
|
||||||
const matched = response?.match(regex) || response?.match(fallbackRegex)
|
*/
|
||||||
const username = matched?.[0].slice(17, -1)
|
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()
|
return username.trim()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,41 +1,97 @@
|
|||||||
import { extractUserNameSas9 } from '../sas9/extractUserNameSas9'
|
import { extractUserNameSas9 } from '../sas9/extractUserNameSas9'
|
||||||
|
|
||||||
describe('Extract username SAS9', () => {
|
describe('Extract username SAS9 English - two word logout handled language', () => {
|
||||||
it('should return username', () => {
|
const logoutWord = 'Log Off'
|
||||||
const response = ` "title": "Log Off SAS User One",`
|
|
||||||
|
it('should return username with space after colon', () => {
|
||||||
|
const response = ` "title": "${logoutWord} SAS User One",`
|
||||||
const username = extractUserNameSas9(response)
|
const username = extractUserNameSas9(response)
|
||||||
|
|
||||||
expect(username).toEqual('SAS User One')
|
expect(username).toEqual('SAS User One')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return username with fallback regex', () => {
|
it('should return username without space after colon', () => {
|
||||||
const response = ` "title": "Logout SAS User One",`
|
const response = ` "title":"${logoutWord} SAS User One",`
|
||||||
const username = extractUserNameSas9(response)
|
const username = extractUserNameSas9(response)
|
||||||
|
|
||||||
expect(username).toEqual('SAS User One')
|
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', () => {
|
it('should return username unknown', () => {
|
||||||
const response = ` invalid",`
|
const response = ` invalid",`
|
||||||
const username = extractUserNameSas9(response)
|
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', () => {
|
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)
|
const username = extractUserNameSas9(response)
|
||||||
|
|
||||||
expect(username).toEqual('SasUserOne')
|
expect(username).toEqual('SasUserOne')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return username with unhandled Spanish language', () => {
|
it('should return username unknown', () => {
|
||||||
const response = ` "title": "Desconectarse SAS User One",`
|
const response = ` invalid",`
|
||||||
const username = extractUserNameSas9(response)
|
const username = extractUserNameSas9(response)
|
||||||
|
|
||||||
// Result won't be perfect but it will work Result will be: ctasasuseone
|
expect(username).toEqual('unknown')
|
||||||
// instead of sasuseone
|
|
||||||
|
|
||||||
expect(username).toEqual('ctarse SAS User One')
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user