Unifying use of clientID utils
Summary: Fixes a couple of typo's and merges two very similar functions into one. Now that there's a single way to create a clientID, we can get more strict about what it is and what it isn't. Reviewed By: passy Differential Revision: D18809741 fbshipit-source-id: 9a68e45bead38cc2917a6d4cd2cf461c309f3ede
This commit is contained in:
committed by
Facebook Github Bot
parent
e1e2978b19
commit
c95ecf6b3e
@@ -10,44 +10,30 @@
|
||||
import Client from '../Client';
|
||||
import BaseDevice from '../devices/BaseDevice';
|
||||
|
||||
type ClientIdConstituents = {
|
||||
app: string;
|
||||
os: string;
|
||||
device: string;
|
||||
device_id: string;
|
||||
};
|
||||
|
||||
export function currentActiveApps(
|
||||
clients: Array<Client>,
|
||||
selectedDevice: null | BaseDevice,
|
||||
): Array<string> {
|
||||
const currentActiveApps: Array<string> = clients
|
||||
.map(({id}: {id: string}) => {
|
||||
const appName = appNameFromClienID(id) || '';
|
||||
const device = deviceFromClienID(id) || '';
|
||||
return {appName, device};
|
||||
const appName = deconstructClientId(id).app || '';
|
||||
const os = deconstructClientId(id).os || '';
|
||||
return {appName, os};
|
||||
})
|
||||
.filter(
|
||||
({device}: {device: string}) =>
|
||||
device && selectedDevice && device == selectedDevice.os,
|
||||
({os}: {os: string}) => os && selectedDevice && os == selectedDevice.os,
|
||||
)
|
||||
.map(client => client.appName);
|
||||
return currentActiveApps;
|
||||
}
|
||||
|
||||
export function appNameFromClienID(id: string): string | undefined {
|
||||
const arr = id.split('#');
|
||||
const appName = arr[0];
|
||||
return appName;
|
||||
}
|
||||
|
||||
export function deviceFromClienID(id: string): string | undefined {
|
||||
const arr = id.split('#');
|
||||
const device = arr[1];
|
||||
return device;
|
||||
}
|
||||
|
||||
export function getCurrentAppName(client: string | undefined | null): string {
|
||||
if (client) {
|
||||
return appNameFromClienID(client) || '';
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
export function buildClientId(clientInfo: {
|
||||
app: string;
|
||||
os: string;
|
||||
@@ -56,3 +42,16 @@ export function buildClientId(clientInfo: {
|
||||
}): string {
|
||||
return `${clientInfo.app}#${clientInfo.os}#${clientInfo.device}#${clientInfo.device_id}`;
|
||||
}
|
||||
|
||||
export function deconstructClientId(clientId: string): ClientIdConstituents {
|
||||
if (!clientId || clientId.split('#').length !== 4) {
|
||||
console.error(`Attempted to deconstruct invalid clientId: "${clientId}"`);
|
||||
}
|
||||
const [app, os, device, device_id] = clientId.split('#');
|
||||
return {
|
||||
app,
|
||||
os,
|
||||
device,
|
||||
device_id,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user