Skip to content

Commit

Permalink
Remove date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Maaaartin committed Nov 20, 2024
1 parent 33cc07a commit 024756d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
42 changes: 37 additions & 5 deletions src/commands/host-transport/listproperties.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
import { findMatches } from '../../util';
import { findMatches, PropertyValue } from '../../util';
import { PropertyMap } from '../../util';
import TransportParseAllCommand from '../abstract/transportParseAll';

export default class ListPropertiesCommand extends TransportParseAllCommand<PropertyMap> {
protected Cmd = 'shell:getprop';
type ParsedPropertyValue = Exclude<PropertyValue, null | undefined>;
const valueParser = (
type: string | undefined,
value: string
): ParsedPropertyValue => {
switch (type) {
case 'bool':
case 'int':
try {
return JSON.parse(value);
} catch {
return value;
}
default:
return value;
}
};

export default class ListPropertiesCommand extends TransportParseAllCommand<
PropertyMap<ParsedPropertyValue>
> {
protected Cmd = 'shell:getprop -T && getprop';

protected parse(value: string): PropertyMap {
return findMatches(value, /^\[([\s\S]*?)\]: \[([\s\S]*?)\]?$/gm, 'map');
protected parse(value: string): PropertyMap<ParsedPropertyValue> {
const matches = findMatches(
value,
/^\[([\s\S]*?)\]: \[([\s\S]*?)\]?$/gm
) as [string, string][];
const typeMap = new Map(matches.slice(0, matches.length / 2));
return new Map(
matches
.slice(matches.length / 2)
.map(([key, value]) => [
key,
valueParser(typeMap.get(key), value)
])
);
}
}
4 changes: 0 additions & 4 deletions src/util/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ export const stringToType = (value: string): PropertyValue => {
}
return parsed;
} catch {
const date = new Date(value);
if (!isNaN(date.getMilliseconds())) {
return date;
}
return value || undefined;
}
};
Expand Down
7 changes: 5 additions & 2 deletions src/util/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ export interface ForwardsObject extends ReversesForwardsBase {

export type PrimitiveType = string | boolean | number | null | undefined;

export type PropertyValue = PrimitiveType | Date;
/**
* @deprecated Will not contain @type {Date} anymore, will replaced by @type {PrimitiveType}
*/
export type PropertyValue = PrimitiveType;

export type PrimitiveDictionary = Record<string, PropertyValue>;

Expand Down Expand Up @@ -416,7 +419,7 @@ export interface CpOptions
copyToTarget?: boolean;
}

export type PropertyMap = Map<string, PropertyValue>;
export type PropertyMap<T = PropertyValue> = Map<string, T>;

export type NonFunctionPropertyNames<T> = {
[K in keyof T]: T[K] extends () => void ? never : K;
Expand Down

0 comments on commit 024756d

Please sign in to comment.