Skip to content

Commit

Permalink
Merge pull request #17 from Maaaartin/fix/wrong-date-parsing
Browse files Browse the repository at this point in the history
Fix/wrong date parsing
  • Loading branch information
Maaaartin authored Nov 30, 2024
2 parents 33cc07a + 7ae06b6 commit 3295990
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 98 deletions.
7 changes: 6 additions & 1 deletion README_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ adb.listDevices((devices) => {

## Change log

### 6.2
### 6.1

`exec` methods accept `string[]` as an argument. Fix for https://github.com/Maaaartin/adb-ts/issues/13.

### 6.2

`PropertyMap` values do not contain `Date` type. This is because parsing date is tricky https://github.com/Maaaartin/adb-ts/issues/15.
`listFeatures` types are determined from `getprop -T` command. Types returned from this method are `number | boolean | string`.
2 changes: 1 addition & 1 deletion __tests__/adbClient/getProp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('Gep prop tests', () => {
const port = await adbMock.start();
const adb = new Client({ noAutoStart: true, port });
const result = await adb.getProp('serial', 'prop');
expect(result).toEqual(date);
expect(result).toEqual(date.toISOString());
} finally {
await adbMock.end();
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/adbClient/getSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('Get setting OKAY tests', () => {
const port = await adbMock.start();
const adb = new Client({ noAutoStart: true, port });
const result = await adb.getSetting('serial', 'system', 'prop');
expect(result).toEqual(date);
expect(result).toEqual(date.toISOString());
} finally {
await adbMock.end();
}
Expand Down
6 changes: 3 additions & 3 deletions __tests__/adbClient/listFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ feature:two=two
feature:three=false
feature:four=true
feature:five=null
feature:six`,
feature:six=`,
raw: true
}
}
Expand Down Expand Up @@ -48,7 +48,7 @@ feature:two=two
feature:three=false
feature:four=true
feature:five=null
feature:six`,
feature:six=`,
raw: true
}
}
Expand Down Expand Up @@ -95,7 +95,7 @@ feature:six`,
feature:three=false
feature:four=true
feature:five=null
feature:six`,
feature:six=`,
raw: true
}
}
Expand Down
26 changes: 12 additions & 14 deletions __tests__/adbClient/listProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@ describe('List properties', () => {
{
cmd: 'shell:getprop',
res: {
value: `[one]: [1]
value: `[one]: [int]
[two]: [string]
[three]: [bool]
[four]: [bool]
[five]: [string]
[six]: [string]
[one]: [1]
[two]: [two]
[three]: [false]
[four]: [true]
[five]: [null]
[six]: []
[seven]: [Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)]`,
`,
raw: true
}
}
Expand All @@ -30,14 +36,8 @@ describe('List properties', () => {
['two', 'two'],
['three', false],
['four', true],
['five', null],
['six', undefined],
[
'seven',
new Date(
'Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)'
)
]
['five', 'null'],
['six', '']
])
);
} finally {
Expand All @@ -56,8 +56,7 @@ describe('List properties', () => {
[three]: [false]
[four]: [true]
[five]: [null]
[six]: []
[seven]: [Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)]`,
[six]: []`,
raw: true
}
}
Expand Down Expand Up @@ -105,8 +104,7 @@ describe('List properties', () => {
[three]: [false]
[four]: [true]
[five]: [null]
[six]: []
[seven]: [Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)]`,
[six]: []`,
raw: true
}
}
Expand Down
17 changes: 4 additions & 13 deletions __tests__/adbClient/listSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ two="two"
three=false
four=true
five=null
six=
seven=Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)`,
six=`,
raw: true
}
}
Expand All @@ -32,13 +31,7 @@ seven=Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)`,
['three', false],
['four', true],
['five', null],
['six', undefined],
[
'seven',
new Date(
'Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)'
)
]
['six', undefined]
])
);
} finally {
Expand Down Expand Up @@ -75,8 +68,7 @@ seven=Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)`,
three=false
four=true
five=null
six=
seven=Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)`,
six=`,
raw: true
}
}
Expand Down Expand Up @@ -123,8 +115,7 @@ seven=Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)`,
three=false
four=true
five=null
six=
seven=Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)`,
six=`,
raw: true
}
}
Expand Down
2 changes: 1 addition & 1 deletion __tests__/device/listFeatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ feature:two=two
feature:three=false
feature:four=true
feature:five=null
feature:six`,
feature:six=`,
raw: true
}
}
Expand Down
20 changes: 10 additions & 10 deletions __tests__/device/listProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ describe('List properties tests', () => {
{
cmd: 'shell:getprop',
res: {
value: `[one]: [1]
value: `[one]: [int]
[two]: [string]
[three]: [bool]
[four]: [bool]
[five]: [string]
[six]: [string]
[one]: [1]
[two]: [two]
[three]: [false]
[four]: [true]
[five]: [null]
[six]: []
[seven]: [Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)]`,
`,
raw: true
}
}
Expand All @@ -40,14 +46,8 @@ describe('List properties tests', () => {
['two', 'two'],
['three', false],
['four', true],
['five', null],
['six', undefined],
[
'seven',
new Date(
'Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)'
)
]
['five', 'null'],
['six', '']
])
);
} finally {
Expand Down
11 changes: 2 additions & 9 deletions __tests__/device/listSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ two="two"
three=false
four=true
five=null
six=
seven=Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)`,
six=`,
raw: true
}
}
Expand All @@ -30,13 +29,7 @@ seven=Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)`,
['three', false],
['four', true],
['five', null],
['six', undefined],
[
'seven',
new Date(
'Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)'
)
]
['six', undefined]
])
);
} finally {
Expand Down
37 changes: 6 additions & 31 deletions __tests__/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,6 @@ describe('String to type', () => {
const result = stringToType('[]');
expect(result).toBe('[]');
});

it('Cast string date as Date', () => {
const result = stringToType('Mon Dec 20 15:05:47 UTC 2021');
expect(result).toEqual(new Date('Mon Dec 20 15:05:47 UTC 2021'));
});

it('Cast iso date as Date', () => {
const date = new Date();
const result = stringToType(date.toISOString());
expect(result).toEqual(date);
});
});

describe('Parse primitive type', () => {
Expand All @@ -125,8 +114,7 @@ describe('Find matches', () => {
[four]: [true]
[five]: [null]
[six]: [[]]
[seven]: [Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)]
[eight]: []`,
[seven]: []`,
/^\[([\s\S]*?)\]: \[([\s\S]*?)\]?$/gm
);
expect(result).toEqual([
Expand All @@ -136,11 +124,7 @@ describe('Find matches', () => {
['four', 'true'],
['five', 'null'],
['six', '[]'],
[
'seven',
'Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)'
],
['eight', '']
['seven', '']
]);
});

Expand All @@ -152,8 +136,7 @@ describe('Find matches', () => {
[four]: [true]
[five]: [null]
[six]: [[]]
[seven]: [Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)]
[eight]: []`,
[seven]: []`,
/^\[([\s\S]*?)\]: \[([\s\S]*?)\]?$/gm,
'map'
);
Expand All @@ -165,13 +148,7 @@ describe('Find matches', () => {
['four', true],
['five', null],
['six', '[]'],
[
'seven',
new Date(
'Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)'
)
],
['eight', undefined]
['seven', undefined]
])
);
});
Expand All @@ -184,8 +161,7 @@ describe('Find matches', () => {
[four]: [true]
[five]: [null]
[six]: [[]]
[seven]: [Sun Jul 17 2022 21:11:48 GMT+0200 (Central European Summer Time)]
[eight]: []`,
[seven]: []`,
/^\[([\s\S]*?)\]: \[([\s\S]*?)\]?$/gm,
'list'
);
Expand All @@ -196,8 +172,7 @@ describe('Find matches', () => {
'four',
'five',
'six',
'seven',
'eight'
'seven'
]);
});
});
Expand Down
40 changes: 34 additions & 6 deletions src/commands/host-transport/listproperties.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
import { findMatches } from '../../util';
import { PropertyMap } from '../../util';
import { findMatches, ListPropertiesMap } from '../../util';
import TransportParseAllCommand from '../abstract/transportParseAll';

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

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

protected parse(value: string): PropertyMap {
return findMatches(value, /^\[([\s\S]*?)\]: \[([\s\S]*?)\]?$/gm, 'map');
protected parse(value: string): ListPropertiesMap {
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
Loading

0 comments on commit 3295990

Please sign in to comment.