@denniskrol/device-profiles 1.20251121.0 → 1.20251122.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -29,13 +29,16 @@ export interface DeviceProfile {
29
29
  [key: string]: any;
30
30
  }
31
31
  export type DeviceProfileFilter = Partial<Record<keyof DeviceProfile, any>> & {};
32
+ export type DeviceProfilePredicate = (profile: DeviceProfile) => boolean;
32
33
  export declare class DeviceProfiles {
33
34
  [key: string]: any;
34
35
  userAgent: string;
35
36
  private _profile;
36
- constructor(filter?: DeviceProfileFilter);
37
+ constructor(filter?: DeviceProfileFilter | DeviceProfilePredicate);
37
38
  static all(): DeviceProfile[];
38
- static random(filter?: DeviceProfileFilter): DeviceProfile;
39
+ static random(): DeviceProfile;
40
+ static random(filter: DeviceProfileFilter): DeviceProfile;
41
+ static random(predicate: DeviceProfilePredicate): DeviceProfile;
39
42
  get profile(): DeviceProfile;
40
43
  }
41
44
  export default DeviceProfiles;
package/dist/index.js CHANGED
@@ -26,6 +26,14 @@ function matchesFilter(profile, filter) {
26
26
  }
27
27
  return true;
28
28
  }
29
+ // Helper to unify object or predicate filtering
30
+ function filterProfiles(input) {
31
+ if (!input || (typeof input === 'object' && Object.keys(input).length === 0))
32
+ return profiles;
33
+ if (typeof input === 'function')
34
+ return profiles.filter(p => input(p));
35
+ return profiles.filter(p => matchesFilter(p, input));
36
+ }
29
37
  function pickWeightedRandom(items) {
30
38
  var _a;
31
39
  if (!items.length) {
@@ -43,9 +51,7 @@ function pickWeightedRandom(items) {
43
51
  }
44
52
  class DeviceProfiles {
45
53
  constructor(filter = {}) {
46
- const filtered = Object.keys(filter).length
47
- ? profiles.filter(p => matchesFilter(p, filter))
48
- : profiles;
54
+ const filtered = filterProfiles(filter);
49
55
  this._profile = pickWeightedRandom(filtered);
50
56
  this.userAgent = this._profile.userAgent; // explicit assignment
51
57
  Object.assign(this, this._profile);
@@ -54,9 +60,7 @@ class DeviceProfiles {
54
60
  return [...profiles];
55
61
  }
56
62
  static random(filter = {}) {
57
- const filtered = Object.keys(filter).length
58
- ? profiles.filter(p => matchesFilter(p, filter))
59
- : profiles;
63
+ const filtered = filterProfiles(filter);
60
64
  return pickWeightedRandom(filtered);
61
65
  }
62
66
  get profile() { return this._profile; }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@denniskrol/device-profiles",
3
- "version": "1.20251121.0",
3
+ "version": "1.20251122.1",
4
4
  "description": "Daily updated browser device profiles",
5
5
  "keywords": [
6
6
  "device-profiles",