A library of sample datasets that can be used during testing and development. Fully typed with TypeScript definitions included.
npm i i45-sample-data --save-dev- ✅ Fully TypeScript supported with comprehensive type definitions
- ✅ ES Modules (ESM) support
- ✅ Tree-shakeable exports with individual imports
- ✅ Zero dependencies for production
- ✅ 18+ dataset types with proper interfaces
- ✅ Optimized bundle size - import only what you need
The package is optimized for tree-shaking to minimize your production bundle:
// ⚠️ Full import - bundles all datasets (~40-50KB minified)import{SampleData}from"i45-sample-data";// ✅ Tree-shakeable - only bundles what you use (~2-10KB each)import{Books}from"i45-sample-data/collections";import{Astronomy}from"i45-sample-data/lists";import{DataHelpers}from"i45-sample-data/helpers";Tips for minimal bundle size:
- Use individual imports (
/collections,/lists, etc.) instead of the main entry point - Only import the datasets you actually use
- Helper methods add ~2KB when imported
Package marked with sideEffects: false for optimal tree-shaking with modern bundlers (webpack, Rollup, Vite, esbuild).
import{SampleData}from"i45-sample-data";// Lists - Simple string arraysconsole.log("Astronomy",SampleData.Lists.Astronomy);console.log("States",SampleData.Lists.States);// Dictionaries - Key-value pairsconsole.log("States",SampleData.Dictionaries.States);console.log("Countries",SampleData.Dictionaries.Countries);// Collections - Arrays of objectsconsole.log("Books",SampleData.Collections.Books);console.log("Students",SampleData.Collections.Students);console.log("Countries",SampleData.Collections.Countries);console.log("TriviaQuestions",SampleData.Collections.TriviaQuestions);console.log("Albums",SampleData.Collections.Albums);console.log("Songs",SampleData.Collections.Songs);console.log("Players",SampleData.Collections.Players);console.log("Recipes",SampleData.Collections.Recipes);console.log("States",SampleData.Collections.States);console.log("DailyWeather",SampleData.Collections.DailyWeather);// Objects - Structured dataconsole.log("KeyCodes",SampleData.Objects.KeyCodes);console.log("States",SampleData.Objects.States);console.log("Movies",SampleData.Objects.Movies);Import only the datasets you need to minimize your bundle size:
// Import specific collectionsimport{Books,TriviaQuestions,Students}from"i45-sample-data/collections";// Import specific listsimport{Astronomy,States}from"i45-sample-data/lists";// Import specific dictionariesimport{Countries}from"i45-sample-data/dictionaries";// Import specific objectsimport{KeyCodes,Movies}from"i45-sample-data/objects";console.log("Books:",Books);console.log("Astronomy:",Astronomy);import{Books,typeBook}from"i45-sample-data/collections";import{Astronomy}from"i45-sample-data/lists";// Type-safe access with minimal bundle sizeconstbooks: Book[]=Books;constastronomyTerms: string[]=Astronomy;console.log(books[0].title);import{SampleData}from"i45-sample-data";// Lists - Simple string arraysconsole.log("Astronomy",SampleData.Lists.Astronomy);console.log("States",SampleData.Lists.States);// Dictionaries - Key-value pairsconsole.log("States",SampleData.Dictionaries.States);console.log("Countries",SampleData.Dictionaries.Countries);// Collections - Arrays of objectsconsole.log("Books",SampleData.Collections.Books);console.log("Students",SampleData.Collections.Students);console.log("Countries",SampleData.Collections.Countries);console.log("TriviaQuestions",SampleData.Collections.TriviaQuestions);console.log("Albums",SampleData.Collections.Albums);console.log("Songs",SampleData.Collections.Songs);console.log("Players",SampleData.Collections.Players);console.log("Recipes",SampleData.Collections.Recipes);console.log("States",SampleData.Collections.States);console.log("DailyWeather",SampleData.Collections.DailyWeather);// Objects - Structured dataconsole.log("KeyCodes",SampleData.Objects.KeyCodes);console.log("States",SampleData.Objects.States);console.log("Movies",SampleData.Objects.Movies);import{SampleData,typeBook,typeTriviaQuestion,typeStudent,}from"i45-sample-data";// Type-safe access to dataconstbooks: Book[]=SampleData.Collections.Books;constfirstBook: Book=books[0];// Intellisense works perfectlyconsole.log(firstBook.title,firstBook.author,firstBook.isbn);// Type-safe function parametersfunctiondisplayQuestion(question: TriviaQuestion): void{console.log(`${question.category}: ${question.question}`);console.log(`Difficulty: ${question.difficulty}`);}// Generic fetch with type parametertypeApiResponse={data: Student[]};constresult=awaitSampleData.fetch<ApiResponse>("https://api.example.com/students");Convenience methods for working with sample data:
import{SampleData}from"i45-sample-data";// Get a random bookconstrandomBook=SampleData.random(SampleData.Collections.Books);// Get 3 random trivia questionsconstquestions=SampleData.randomMultiple(SampleData.Collections.TriviaQuestions,3);// Shuffle the states listconstshuffledStates=SampleData.shuffle(SampleData.Lists.States);Import DataHelpers for advanced data manipulation utilities:
import{TriviaQuestions,Books}from"i45-sample-data/collections";import{DataHelpers}from"i45-sample-data/helpers";// Filter by difficultyconsteasyQuestions=DataHelpers.filterBy(TriviaQuestions,"difficulty","easy");// Filter with custom functionconstintermediateQuestions=DataHelpers.filterWhere(TriviaQuestions,(i)=>i.difficulty=="easy"||i.difficulty=="medium");// Filter where property values are between a min and max value.constlongBooks=DataHelpers.range(Books,"pages",300,400);// Search across multiple fieldsconstscience=DataHelpers.search(TriviaQuestions,"science",["category","question",]);// Group by difficultyconstgrouped=DataHelpers.groupBy(TriviaQuestions,"difficulty");// { easy: [...], medium: [...], hard: [...] }// Paginate resultsconstpage1=DataHelpers.paginate(TriviaQuestions,1,5);// { data: [...5 items], page: 1, total: 10, hasNext: true, ... }// Sort by propertyconstsorted=DataHelpers.sortBy(TriviaQuestions,"difficulty","asc");// Get unique valuesconstdifficulties=DataHelpers.unique(TriviaQuestions,"difficulty");// Extract specific fieldsconstsimplified=DataHelpers.pluck(TriviaQuestions,"question","difficulty");Available DataHelper methods:
filterBy()- Filter by property valuesearch()- Search across string fieldsfilterWhere()- Filter with custom functionrange()- Filter by property value within min and maxgroupBy()- Group by propertycountBy()- Count occurrencessortBy()- Sort by propertypaginate()- Paginate with metadataunique()- Get unique valuespluck()- Extract properties
All TypeScript interfaces are exported for use in your projects:
Book- ISBN, title, author, publisher detailsTriviaQuestion- Trivia questions with difficulty and categoryAlbum- Music album informationSong- Song metadata with audio featuresPlayer- Soccer player dataRecipe- Cooking recipes with ingredients and instructionsStudent- Student records with gradesState- US state informationCountry- Country data with currency and languageDailyWeather- Weather forecast dataKeyCodes- Keyboard key code mappingsMovie- Movie metadata- And more...
console.log(SampleData.Collections.TriviaQuestions);// output[{type: "multiple",difficulty: "easy",category: "Science & Nature",question:
"The element involved in making human blood red is which of the following?",correct_answer: "Iron",incorrect_answers: ["Copper","Iridium","Gold"],},{type: "multiple",difficulty: "easy",category: "Entertainment: Video Games",question:
"How many flagship monsters appear in Monster Hunter Gernerations?",correct_answer: "4",incorrect_answers: ["3","5","6"],},{type: "multiple",difficulty: "hard",category: "Entertainment: Video Games",question:
"'Gimmick!' is a Japanese Famicom game in the cartridge. What is it called?",correct_answer: "FME-7",incorrect_answers: ["VRC6","VRC7","MMC5"],},{type: "boolean",difficulty: "medium",category: "Animals",question: "Finnish Lapphund dogs were used for herding reindeer.",correct_answer: "True",incorrect_answers: ["False"],},];import"./App.css";import{SampleData,typeTriviaQuestion}from"i45-sample-data";functionApp(){constquestions: TriviaQuestion[]=SampleData.Collections.TriviaQuestions;return(<><div>{questions.map((question,index)=>(<divid={`q-${index}`}key={index}className="question-div"><h3>{question.question}</h3><p>Category: {question.category}</p><p>Difficulty: {question.difficulty}</p><p>Type: {question.type}</p><p>CorrectAnswer: {question.correct_answer}</p><p>IncorrectAnswers: {question.incorrect_answers.join(", ")}</p></div>))}</div></>);}exportdefaultApp;The from method makes a get request using the fetch api and returns the results in JSON format.
The apiKey parameter is optional - only provide it if your API requires authentication.
Signature:SampleData.from(uri: string, apiKey?: string): Promise<any>
NOTE: This method makes an HTTP Get request and, if an API key is provided, passes it in the x-api-key request header. To make an HTTP Post request, or to include other headers such as custom content types, see the section API Call with Custom Headers.
import{SampleData}from"i45-sample-data";// Make an async API call to a public endpoint (no API key needed)constdata=awaitSampleData.from("https://jsonplaceholder.typicode.com/posts");console.log(data);// Or with an API key for authenticated endpointsconstprotectedData=awaitSampleData.from("https://api.example.com/protected","YOUR_API_KEY");console.log(protectedData);options object, and returns a typed Response.
See the Fetch API MDN Web Docs for more information.
import{SampleData}from"i45-sample-data";// With TypeScript, you can specify the response typetypePost={id: number;title: string;body: string;userId: number};constdata=awaitSampleData.fetch<Post[]>("https://jsonplaceholder.typicode.com/posts",{headers: {"Content-Type": "application/json","x-api-key": "YOUR_API_KEY",},});// data is now typed as Post[]console.log("Data from API",data);The following deprecated properties are still available but not recommended:
// ⚠️ Deprecated - Use Collections insteadSampleData.JsonData.Books;// ⚠️ Deprecated - Use Dictionaries insteadSampleData.KeyValueLists.States;// ✅ RecommendedSampleData.Collections.Books;SampleData.Dictionaries.States;ISC