diff --git a/index.js b/index.js index c0418a7..aa98f39 100644 --- a/index.js +++ b/index.js @@ -30,34 +30,44 @@ const all = (() => { iphone.map(v => { v.Type = 'iphone'; return v; }), ipod_touch.map(v => { v.Type = 'ipod_touch'; return v; }) ); - total.forEach(v => { - v.Models.forEach(m => { - m.Model.forEach(mid => { - let item = { - Type: v.Type, - Generation: v.Generation, - ANumber: v.ANumber, - Bootrom: v.Bootrom, - Variant: v.Variant, - FCCID: v.FCCID, - InternalName: v.InternalName, - Identifier: v.Identifier, - Color: m.Color, - Storage: m.Storage, - Model: mid - }; - if (m.CaseMaterial) { - item.CaseMaterial = m.CaseMaterial; - } - l.push(item); + // Some devices don't have any models information defined, we still want them to be listed + if(v.Models.length === 0) { + let item = createItem(v, null, null); + l.push(item); + } else { + v.Models.forEach(m => { + m.Model.forEach(mid => { + let item = createItem(v,m, mid); + l.push(item); + }); }); - }); + } }); return l; })(); +function createItem(device, model, modelID) { + let item = { + Type: device.Type, + Generation: device.Generation, + ANumber: device.ANumber, + Bootrom: device.Bootrom, + Variant: device.Variant, + FCCID: device.FCCID, + InternalName: device.InternalName, + Identifier: device.Identifier, + Color: model ? model.Color : '', + Storage: model ? model.Storage : '', + Model: modelID ? modelID : '' + }; + if (model && model.CaseMaterial) { + item.CaseMaterial = model.CaseMaterial; + } + return item; +} + function deviceTypes() { return 'airpods,apple_tv,apple_watch,homepod,ipad,ipad_air,ipad_pro,ipad_mini,iphone,ipod_touch'.split(','); } diff --git a/test.js b/test.js index 44055c4..baf852f 100644 --- a/test.js +++ b/test.js @@ -10,18 +10,8 @@ test('deviceTypes()', t => { }); test('devices()', t => { - let list = devices.deviceTypes(); - t.is(typeof devices.devices, 'function'); t.truthy(Array.isArray(devices.devices())); - -// t.is(devices.devices().length, 728); -// t.is(devices.devices(list[0]).length, 5); -// t.is(devices.devices(list[1]).length, 30); -// t.is(devices.devices(list[2]).length, 193); -// t.is(devices.devices(list[3]).length, 102); -// t.is(devices.devices(list[4]).length, 328); -// t.is(devices.devices(list[5]).length, 69); }); test('devices() device', t => { @@ -32,7 +22,6 @@ test('devices() device', t => { t.is(typeof d1.Generation, 'string'); t.is(typeof d1.ANumber, 'string'); t.is(typeof d1.Bootrom, 'string'); -// t.is(typeof d1.Variant, 'string'); t.is(typeof d1.FCCID, 'string'); t.is(typeof d1.InternalName, 'string'); t.is(typeof d1.Identifier, 'string'); @@ -44,7 +33,6 @@ test('devices() device', t => { t.is(typeof d2.Generation, 'string'); t.is(Array.isArray(d2.ANumber), true); t.is(Array.isArray(d2.Bootrom), true); -// t.is(typeof d2.Variant, 'string'); t.is(Array.isArray(d2.FCCID) || typeof d2.FCCID === 'string', true); t.is(typeof d2.InternalName, 'string'); t.is(typeof d2.Identifier, 'string'); @@ -56,77 +44,77 @@ test('devices() device', t => { test('generations()', t => { t.is(typeof devices.generations, 'function'); t.truthy(Array.isArray(devices.generations())); - t.is(devices.generations().length, 94); + t.is(devices.generations().length, 98 ); t.is(devices.generations('iphone').length, 38); - t.throws(function() { devices.generations('invalidType') }); + t.throws(function() { devices.generations('invalidType'); }); }); test('anumbers()', t => { t.is(typeof devices.anumbers, 'function'); t.truthy(Array.isArray(devices.anumbers())); - t.is(devices.anumbers().length, 287); + t.is(devices.anumbers().length, 290); t.is(devices.anumbers('iphone').length, 126); - t.throws(function() { devices.anumbers('invalidType') }); + t.throws(function() { devices.anumbers('invalidType'); }); }); test('fccids()', t => { t.is(typeof devices.fccids, 'function'); t.truthy(Array.isArray(devices.fccids())); - t.is(devices.fccids().length, 194); + t.is(devices.fccids().length, 196); t.is(devices.fccids('iphone').length, 56); - t.throws(function() { devices.fccids('invalidType') }); + t.throws(function() { devices.fccids('invalidType'); }); }); test('internalNames()', t => { t.is(typeof devices.internalNames, 'function'); t.truthy(Array.isArray(devices.internalNames())); - t.is(devices.internalNames().length, 174); + t.is(devices.internalNames().length, 184); t.is(devices.internalNames('iphone').length, 54); - t.throws(function() { devices.internalNames('invalidType') }); + t.throws(function() { devices.internalNames('invalidType'); }); }); test('identifiers()', t => { t.is(typeof devices.identifiers, 'function'); t.truthy(Array.isArray(devices.identifiers())); - t.is(devices.identifiers().length, 166); + t.is(devices.identifiers().length, 176); t.is(devices.identifiers('iphone').length, 49); - t.throws(function() { devices.identifiers('invalidType') }); + t.throws(function() { devices.identifiers('invalidType'); }); }); test('colors()', t => { t.is(typeof devices.colors, 'function'); t.truthy(Array.isArray(devices.colors())); - t.is(devices.colors().length, 30); + t.is(devices.colors().length, 31); t.is(devices.colors('iphone').length, 23); - t.throws(function() { devices.colors('invalidType') }); + t.throws(function() { devices.colors('invalidType'); }); }); test('storages()', t => { t.is(typeof devices.storages, 'function'); t.truthy(Array.isArray(devices.storages())); - t.is(devices.storages().length, 13); + t.is(devices.storages().length, 14); t.is(devices.storages('iphone').length, 9); - t.throws(function() { devices.storages('invalidType') }); + t.throws(function() { devices.storages('invalidType'); }); }); test('models()', t => { t.is(typeof devices.models, 'function'); t.truthy(Array.isArray(devices.models())); -// t.is(devices.models().length, 714); -// t.is(devices.models('iphone').length, 317); - t.throws(function() { devices.models('invalidType') }); + t.is(devices.models().length, 4147); + t.is(devices.models('iphone').length, 2652); + t.throws(function() { devices.models('invalidType'); }); }); test('deviceByGeneration()', t => { let find = 'iPhone 6s Plus'; t.is(typeof devices.deviceByGeneration, 'function'); - t.throws(function() { devices.deviceByGeneration() }); + t.throws(function() { devices.deviceByGeneration(); }); t.is(typeof devices.deviceByGeneration(find), 'object'); t.is(devices.deviceByGeneration(find).length > 0, true); t.is(devices.deviceByGeneration(find, 'iphone').length > 0, true); t.is(devices.deviceByGeneration(find, 'ipad').length, 0); - t.throws(function() { devices.deviceByGeneration(find, 'invalidType') }); + t.throws(function() { devices.deviceByGeneration(find, 'invalidType'); }); t.is(devices.deviceByGeneration(find.substr(0, 11)).length, 0); t.is(devices.deviceByGeneration(find.substr(0, 11), null, { contains: true }).length > 0, true); @@ -143,15 +131,14 @@ test('deviceByANumber()', t => { let find = 'A1634'; t.is(typeof devices.deviceByANumber, 'function'); - t.throws(function() { devices.deviceByANumber() }); + t.throws(function() { devices.deviceByANumber(); }); t.is(typeof devices.deviceByANumber(find), 'object'); t.is(devices.deviceByANumber(find).length > 0, true); t.is(devices.deviceByANumber(find, 'iphone').length > 0, true); t.is(devices.deviceByANumber(find, 'ipad').length, 0); - t.throws(function() { devices.deviceByANumber(find, 'invalidType') }); + t.throws(function() { devices.deviceByANumber(find, 'invalidType'); }); t.is(devices.deviceByANumber(find.substr(0, 4)).length, 0); -// t.is(devices.deviceByANumber(find.substr(0, 4), null, { contains: true }).length, 63); t.is(devices.deviceByANumber(find.substr(0, 4), null, { contains: true }).length > 0, true); t.is(devices.deviceByANumber(find.toLowerCase()).length, 0); t.is(devices.deviceByANumber(find.toLowerCase(), null, { caseInsensitive: true }).length > 0, true); @@ -159,7 +146,6 @@ test('deviceByANumber()', t => { t.is(devices.deviceByANumber(find.substr(0, 4).toLowerCase()).length, 0); t.is(devices.deviceByANumber(find.substr(0, 4).toLowerCase(), null, { contains: true }).length, 0); t.is(devices.deviceByANumber(find.substr(0, 4).toLowerCase(), null, { caseInsensitive: true }).length, 0); -// t.is(devices.deviceByANumber(find.substr(0, 4).toLowerCase(), null, { contains: true, caseInsensitive: true }).length, 63); t.is(devices.deviceByANumber(find.substr(0, 4).toLowerCase(), null, { contains: true, caseInsensitive: true }).length > 0, true); }); @@ -167,15 +153,15 @@ test('deviceByFCCID()', t => { let find = 'BCG-E2944A'; t.is(typeof devices.deviceByFCCID, 'function'); - t.throws(function() { devices.deviceByFCCID() }); + t.throws(function() { devices.deviceByFCCID(); }); t.is(typeof devices.deviceByFCCID(find), 'object'); t.is(devices.deviceByFCCID(find).length > 0, true); t.is(devices.deviceByFCCID(find, 'iphone').length > 0, true); t.is(devices.deviceByFCCID(find, 'ipad').length, 0); - t.throws(function() { devices.deviceByFCCID(find, 'invalidType') }); + t.throws(function() { devices.deviceByFCCID(find, 'invalidType'); }); t.is(devices.deviceByFCCID(find.substr(0, 8)).length, 0); -// t.is(devices.deviceByFCCID(find.substr(0, 8), null, { contains: true }).length, 48); + // t.is(devices.deviceByFCCID(find.substr(0, 8), null, { contains: true }).length, 48); t.is(devices.deviceByFCCID(find.substr(0, 8), null, { contains: true }).length > 0, true); t.is(devices.deviceByFCCID(find.toLowerCase()).length, 0); t.is(devices.deviceByFCCID(find.toLowerCase(), null, { caseInsensitive: true }).length > 0, true); @@ -183,7 +169,7 @@ test('deviceByFCCID()', t => { t.is(devices.deviceByFCCID(find.substr(0, 8).toLowerCase()).length, 0); t.is(devices.deviceByFCCID(find.substr(0, 8).toLowerCase(), null, { contains: true }).length, 0); t.is(devices.deviceByFCCID(find.substr(0, 8).toLowerCase(), null, { caseInsensitive: true }).length, 0); -// t.is(devices.deviceByFCCID(find.substr(0, 8).toLowerCase(), null, { contains: true, caseInsensitive: true }).length, 48); + // t.is(devices.deviceByFCCID(find.substr(0, 8).toLowerCase(), null, { contains: true, caseInsensitive: true }).length, 48); t.is(devices.deviceByFCCID(find.substr(0, 8).toLowerCase(), null, { contains: true, caseInsensitive: true }).length > 0, true); }); @@ -191,12 +177,12 @@ test('deviceByInternalName()', t => { let find = 'N66AP'; t.is(typeof devices.deviceByInternalName, 'function'); - t.throws(function() { devices.deviceByInternalName() }); + t.throws(function() { devices.deviceByInternalName(); }); t.is(typeof devices.deviceByInternalName(find), 'object'); t.is(devices.deviceByInternalName(find).length > 0, true); t.is(devices.deviceByInternalName(find, 'iphone').length > 0, true); t.is(devices.deviceByInternalName(find, 'ipad').length, 0); - t.throws(function() { devices.deviceByInternalName(find, 'invalidType') }); + t.throws(function() { devices.deviceByInternalName(find, 'invalidType'); }); t.is(devices.deviceByInternalName(find.substr(0, 4)).length, 0); t.is(devices.deviceByInternalName(find.substr(0, 4), null, { contains: true }).length > 0, true); @@ -213,15 +199,15 @@ test('deviceByIdentifier()', t => { let find = 'iPhone8,2'; t.is(typeof devices.deviceByIdentifier, 'function'); - t.throws(function() { devices.deviceByIdentifier() }); + t.throws(function() { devices.deviceByIdentifier(); }); t.is(typeof devices.deviceByIdentifier(find), 'object'); t.is(devices.deviceByIdentifier(find).length > 0, true); t.is(devices.deviceByIdentifier(find, 'iphone').length > 0, true); t.is(devices.deviceByIdentifier(find, 'ipad').length, 0); - t.throws(function() { devices.deviceByIdentifier(find, 'invalidType') }); + t.throws(function() { devices.deviceByIdentifier(find, 'invalidType'); }); t.is(devices.deviceByIdentifier(find.substr(0, 8)).length, 0); -// t.is(devices.deviceByIdentifier(find.substr(0, 8), null, { contains: true }).length, 56); + // t.is(devices.deviceByIdentifier(find.substr(0, 8), null, { contains: true }).length, 56); t.is(devices.deviceByIdentifier(find.substr(0, 8), null, { contains: true }).length > 0, true); t.is(devices.deviceByIdentifier(find.toLowerCase()).length, 0); t.is(devices.deviceByIdentifier(find.toLowerCase(), null, { caseInsensitive: true }).length > 0, true); @@ -229,7 +215,7 @@ test('deviceByIdentifier()', t => { t.is(devices.deviceByIdentifier(find.substr(0, 8).toLowerCase()).length, 0); t.is(devices.deviceByIdentifier(find.substr(0, 8).toLowerCase(), null, { contains: true }).length, 0); t.is(devices.deviceByIdentifier(find.substr(0, 8).toLowerCase(), null, { caseInsensitive: true }).length, 0); -// t.is(devices.deviceByIdentifier(find.substr(0, 8).toLowerCase(), null, { contains: true, caseInsensitive: true }).length, 56); + // t.is(devices.deviceByIdentifier(find.substr(0, 8).toLowerCase(), null, { contains: true, caseInsensitive: true }).length, 56); t.is(devices.deviceByIdentifier(find.substr(0, 8).toLowerCase(), null, { contains: true, caseInsensitive: true }).length > 0, true); }); @@ -237,12 +223,12 @@ test('deviceByColor()', t => { let find = 'Rose Gold'; t.is(typeof devices.deviceByColor, 'function'); - t.throws(function() { devices.deviceByColor() }); + t.throws(function() { devices.deviceByColor(); }); t.is(typeof devices.deviceByColor(find), 'object'); t.is(devices.deviceByColor(find).length > 0, true); t.is(devices.deviceByColor(find, 'iphone').length > 0, true); t.is(devices.deviceByColor(find, 'ipad').length > 0, false); - t.throws(function() { devices.deviceByColor(find, 'invalidType') }); + t.throws(function() { devices.deviceByColor(find, 'invalidType'); }); t.is(devices.deviceByColor(find.substr(0, 8)).length, 0); t.is(devices.deviceByColor(find.substr(0, 8), null, { contains: true }).length > 0, true); @@ -259,12 +245,12 @@ test('deviceByStorage()', t => { let find = '128 GB'; t.is(typeof devices.deviceByStorage, 'function'); - t.throws(function() { devices.deviceByStorage() }); + t.throws(function() { devices.deviceByStorage(); }); t.is(typeof devices.deviceByStorage(find), 'object'); t.is(devices.deviceByStorage(find).length > 0, true); t.is(devices.deviceByStorage(find, 'iphone').length > 0, true); t.is(devices.deviceByStorage(find, 'ipad').length > 0, true); - t.throws(function() { devices.deviceByStorage(find, 'invalidType') }); + t.throws(function() { devices.deviceByStorage(find, 'invalidType'); }); t.is(devices.deviceByStorage(find.substr(0, 4)).length, 0); t.is(devices.deviceByStorage(find.substr(0, 4), null, { contains: true }).length > 0, true); @@ -281,12 +267,12 @@ test('deviceByModel()', t => { let find = 'MKU52'; t.is(typeof devices.deviceByModel, 'function'); - t.throws(function() { devices.deviceByModel() }); + t.throws(function() { devices.deviceByModel(); }); t.is(typeof devices.deviceByModel(find), 'object'); t.is(devices.deviceByModel(find).length, 1); t.is(devices.deviceByModel(find, 'iphone').length, 1); t.is(devices.deviceByModel(find, 'ipad').length, 0); - t.throws(function() { devices.deviceByModel(find, 'invalidType') }); + t.throws(function() { devices.deviceByModel(find, 'invalidType'); }); t.is(devices.deviceByModel(find.substr(0, 3)).length, 0); t.is(devices.deviceByModel(find.substr(0, 3), null, { contains: true }).length > 0, true); @@ -303,10 +289,18 @@ test('generationByIdentifier()', t => { let find = 'iPhone8,2'; t.is(typeof devices.generationByIdentifier, 'function'); - t.throws(function() { devices.generationByIdentifier() }); + t.throws(function() { devices.generationByIdentifier(); }); t.is(typeof devices.generationByIdentifier(find), 'string'); t.is(devices.generationByIdentifier(find), 'iPhone 6s Plus'); t.is(devices.generationByIdentifier(find, 'iphone'), 'iPhone 6s Plus'); t.is(devices.generationByIdentifier(find, 'ipad'), undefined); - t.throws(function() { devices.generationByIdentifier(find, 'invalidType') }); + t.throws(function() { devices.generationByIdentifier(find, 'invalidType'); }); }); + +test('iPad Pro (12.9-inch) (5th generation) which does not have any Model', t => { + let find = 'iPad13,8'; + const ipadPros = devices.devices('ipad_pro'); + t.is(typeof ipadPros, 'object'); + t.is(ipadPros.filter(t => t.Identifier === find).length, 1); + t.is(ipadPros.filter(t => t.Identifier === find)[0].Generation, 'iPad Pro (12.9-inch) (5th generation)'); +}); \ No newline at end of file