@sambuddha_npm_dev/pagecraft 1.0.0 → 1.0.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.
Files changed (2) hide show
  1. package/README.md +79 -19
  2. package/package.json +4 -1
package/README.md CHANGED
@@ -1,22 +1,23 @@
1
1
  # PageCraft
2
2
 
3
- PageCraft is a small, dependency-free JavaScript utility for filtering, searching, sorting, and paginating arrays. It uses ES modules and runs on Node.js 18 or newer.
3
+ PageCraft is a dependency-free JavaScript utility for filtering, searching, sorting, and paginating arrays of records. It uses ES modules and supports Node.js 18 or newer.
4
4
 
5
5
  ## Install
6
6
 
7
7
  ```sh
8
- npm install pagecraft
8
+ npm install @sambuddha_npm_dev/pagecraft
9
9
  ```
10
10
 
11
11
  ## Quick start
12
12
 
13
13
  ```js
14
- import { paginate } from "pagecraft";
14
+ import { paginate } from "@sambuddha_npm_dev/pagecraft";
15
15
 
16
16
  const users = [
17
17
  { id: 1, name: "Sam", role: "student" },
18
18
  { id: 2, name: "Alex", role: "admin" },
19
- { id: 3, name: "Samira", role: "student" }
19
+ { id: 3, name: "Samira", role: "student" },
20
+ { id: 4, name: "Jordan", role: "student" }
20
21
  ];
21
22
 
22
23
  const result = paginate(users, {
@@ -28,44 +29,101 @@ const result = paginate(users, {
28
29
  order: "asc"
29
30
  });
30
31
 
31
- console.log(result.data); // Sam, Samira
32
+ console.log(result.data.map((user) => user.name)); // ["Sam", "Samira"]
32
33
  console.log(result.pagination);
33
34
  ```
34
35
 
35
36
  ## API
36
37
 
37
- ### `paginate(data, options)`
38
+ ### `paginate(data, options = {})`
38
39
 
39
- Options are `page` (default `1`), `limit` (default `10`, maximum `100`), `filters` (exact matches; all must match), `search` (`{ query, fields }`, case-insensitive substring match), `sortBy` (property name), and `order` (`"asc"` or `"desc"`, default `"asc"`). Processing order is filtering, searching, sorting, then pagination. Inputs are not mutated. An out-of-range page returns an empty `data` array; empty input has `totalPages: 0`.
40
+ Processes an array in this order: filters, search, sorting, then pagination. It returns a new array and does not mutate the input.
40
41
 
41
- Result shape:
42
+ | Option | Type | Default | Behavior |
43
+ |---|---|---|---|
44
+ | `page` | integer | `1` | One-based page number; must be positive. |
45
+ | `limit` | integer | `10` | Items per page; must be from `1` to `100`. |
46
+ | `filters` | object | none | Each property must match a record property using strict equality; every filter must match. |
47
+ | `search` | `{ query, fields }` | none | Case-insensitive substring search in the listed fields. `query` is a string and `fields` is an array of strings. |
48
+ | `sortBy` | string | none | Record property to sort by. An empty string does not enable sorting. |
49
+ | `order` | `"asc"` or `"desc"` | `"asc"` | Sort direction. |
50
+
51
+ Example result:
42
52
 
43
53
  ```js
44
54
  {
45
- data: [],
55
+ data: [{ id: 1, name: "Sam", role: "student" }],
46
56
  pagination: {
47
- page: 1, limit: 10, totalItems: 0, totalPages: 0,
48
- hasNextPage: false, hasPreviousPage: false
57
+ page: 1,
58
+ limit: 10,
59
+ totalItems: 1,
60
+ totalPages: 1,
61
+ hasNextPage: false,
62
+ hasPreviousPage: false
49
63
  }
50
64
  }
51
65
  ```
52
66
 
53
- ### Helpers
67
+ `totalItems` and `totalPages` describe the filtered, searched, and sorted records before slicing. A page beyond the last page returns an empty `data` array. Empty input has `totalPages: 0`.
68
+
69
+ ### `filterData(data, filters = {})`
70
+
71
+ Returns records matching every filter with strict equality (`===`).
72
+
73
+ ```js
74
+ import { filterData } from "@sambuddha_npm_dev/pagecraft";
75
+
76
+ const students = filterData(
77
+ [{ name: "Sam", role: "student" }, { name: "Alex", role: "admin" }],
78
+ { role: "student" }
79
+ );
80
+ ```
81
+
82
+ ### `searchData(data, query, fields)`
54
83
 
55
- - `filterData(data, filters)` returns items that exactly match every filter.
56
- - `searchData(data, query, fields)` searches selected properties. A blank query returns a shallow copy of the input array.
57
- - `sortData(data, field, order)` returns a sorted copy; numbers compare numerically and other values compare as case-insensitive strings. Missing values sort last in ascending order.
58
- - `validateQuery(options)` checks pagination and sorting options and returns normalized `{ page, limit, order }` values.
84
+ Returns records where at least one selected field contains the trimmed query, case-insensitively. Field values are converted to strings. A blank query returns a shallow copy of the input array.
59
85
 
60
- Invalid arrays, options, page values, limits, orders, and search configurations throw `TypeError` or `RangeError` with a descriptive message.
86
+ ```js
87
+ import { searchData } from "@sambuddha_npm_dev/pagecraft";
88
+
89
+ const matches = searchData([{ name: "Sam" }, { name: "Alex" }], "SAM", ["name"]);
90
+ ```
91
+
92
+ ### `sortData(data, field, order = "asc")`
93
+
94
+ Returns a sorted copy. When both values are numbers they sort numerically; otherwise they compare as case-insensitive strings with numeric-aware ordering. Null or missing values sort last ascending and first descending.
95
+
96
+ ```js
97
+ import { sortData } from "@sambuddha_npm_dev/pagecraft";
98
+
99
+ const sorted = sortData([{ name: "Sam" }, { name: "Alex" }], "name", "desc");
100
+ ```
101
+
102
+ ### `validateQuery(options = {})`
103
+
104
+ Validates the `page`, `limit`, and `order` values and checks the types of `sortBy`, `filters`, and `search`. It returns normalized pagination and order defaults:
105
+
106
+ ```js
107
+ import { validateQuery } from "@sambuddha_npm_dev/pagecraft";
108
+
109
+ const query = validateQuery({ page: 2, limit: 5 });
110
+ // { page: 2, limit: 5, order: "asc" }
111
+ ```
112
+
113
+ Invalid top-level inputs and malformed filters/search options throw `TypeError`; invalid page, limit, or order values throw `RangeError`. `paginate`, `filterData`, `searchData`, and `sortData` require `data` to be an array. Filtering and searching are intended for arrays of object records; non-object entries do not match those operations.
61
114
 
62
115
  ## Express example
63
116
 
64
117
  ```js
65
118
  import express from "express";
66
- import { paginate } from "pagecraft";
119
+ import { paginate } from "@sambuddha_npm_dev/pagecraft";
67
120
 
68
121
  const app = express();
122
+ const users = [
123
+ { id: 1, name: "Sam", role: "student" },
124
+ { id: 2, name: "Alex", role: "admin" }
125
+ ];
126
+
69
127
  app.get("/users", (req, res) => {
70
128
  try {
71
129
  const result = paginate(users, {
@@ -79,6 +137,8 @@ app.get("/users", (req, res) => {
79
137
  res.status(400).json({ error: error.message });
80
138
  }
81
139
  });
140
+
141
+ app.listen(3000);
82
142
  ```
83
143
 
84
144
  ## Development
@@ -88,7 +148,7 @@ npm test
88
148
  npm pack
89
149
  ```
90
150
 
91
- Tests use Node's built-in test runner. PageCraft only operates on arrays already in memory; use database-level queries for large datasets.
151
+ The tests use Node.js's built-in test runner. PageCraft processes data in memory; for large datasets, use database-level filtering and pagination.
92
152
 
93
153
  ## License
94
154
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sambuddha_npm_dev/pagecraft",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Small in-memory helpers for pagination, sorting, filtering, searching, and query validation.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -28,5 +28,8 @@
28
28
  "node": ">=18"
29
29
  },
30
30
  "license": "MIT",
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
31
34
  "keywords": ["pagination", "sorting", "filtering", "search", "query"]
32
35
  }