web-one 0.0.6 → 0.0.7

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/lib/index.js CHANGED
@@ -333,13 +333,13 @@ function buildSortFromParams(params) {
333
333
  return buildSort(undefined);
334
334
  }
335
335
  exports.buildSortFromParams = buildSortFromParams;
336
- function renderSort(field, sort) {
336
+ function getSortType(field, sort) {
337
337
  if (field === sort.field) {
338
- return sort.type === "-" ? "<i class='sort-down'></i>" : "<i class='sort-up'></i>";
338
+ return sort.type === "-" ? "-" : "+";
339
339
  }
340
- return "";
340
+ return undefined;
341
341
  }
342
- exports.renderSort = renderSort;
342
+ exports.getSortType = getSortType;
343
343
  function buildSortSearch(params, fields, sortStr) {
344
344
  const search = removeSort(params);
345
345
  const sort = buildSort(sortStr);
@@ -348,7 +348,7 @@ function buildSortSearch(params, fields, sortStr) {
348
348
  for (let i = 0; i < fields.length; i++) {
349
349
  sorts[fields[i]] = {
350
350
  url: prefix + resources.sort + "=" + getSortString(fields[i], sort),
351
- tag: renderSort(fields[i], sort),
351
+ type: getSortType(fields[i], sort),
352
352
  };
353
353
  }
354
354
  return sorts;
@@ -700,3 +700,15 @@ function cloneArray(arr) {
700
700
  return arr.map(item => (Object.assign({}, item)));
701
701
  }
702
702
  exports.cloneArray = cloneArray;
703
+ function getOffset(limit, page, firstLimit) {
704
+ const p = page && page > 0 ? page : 1;
705
+ if (firstLimit && firstLimit > 0) {
706
+ const offset = limit * (p - 2) + firstLimit;
707
+ return offset < 0 ? 0 : offset;
708
+ }
709
+ else {
710
+ const offset = limit * (p - 1);
711
+ return offset < 0 ? 0 : offset;
712
+ }
713
+ }
714
+ exports.getOffset = getOffset;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-one",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "Web utilities",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -88,7 +88,7 @@ export interface Sort {
88
88
  }
89
89
  export interface SortType {
90
90
  url: string
91
- tag: string
91
+ type?: "+" | "-"
92
92
  }
93
93
  export interface SortMap {
94
94
  [key: string]: SortType
@@ -317,11 +317,11 @@ export function buildSortFromParams(params: Record<string, string | string[] | u
317
317
  }
318
318
  return buildSort(undefined)
319
319
  }
320
- export function renderSort(field: string, sort: Sort): string {
320
+ export function getSortType(field: string, sort: Sort): "-" | "+" | undefined {
321
321
  if (field === sort.field) {
322
- return sort.type === "-" ? "<i class='sort-down'></i>" : "<i class='sort-up'></i>"
322
+ return sort.type === "-" ? "-" : "+"
323
323
  }
324
- return ""
324
+ return undefined
325
325
  }
326
326
  export function buildSortSearch(params: Record<string, string | string[] | undefined>, fields: string[], sortStr?: string): SortMap {
327
327
  const search = removeSort(params)
@@ -331,12 +331,11 @@ export function buildSortSearch(params: Record<string, string | string[] | undef
331
331
  for (let i = 0; i < fields.length; i++) {
332
332
  sorts[fields[i]] = {
333
333
  url: prefix + resources.sort + "=" + getSortString(fields[i], sort),
334
- tag: renderSort(fields[i], sort),
334
+ type: getSortType(fields[i], sort),
335
335
  }
336
336
  }
337
337
  return sorts
338
338
  }
339
-
340
339
  export function formatInteger(v?: number | null, groupSeparator: string = ","): string {
341
340
  if (v == null || !Number.isFinite(v)) {
342
341
  return ""
@@ -701,3 +700,14 @@ function getChildren(m: Category, all: Category[]) {
701
700
  export function cloneArray<T>(arr: T[]): T[] {
702
701
  return arr.map(item => ({ ...item }));
703
702
  }
703
+
704
+ export function getOffset(limit: number, page?: number, firstLimit?: number): number {
705
+ const p = page && page > 0 ? page : 1
706
+ if (firstLimit && firstLimit > 0) {
707
+ const offset = limit * (p - 2) + firstLimit
708
+ return offset < 0 ? 0 : offset
709
+ } else {
710
+ const offset = limit * (p - 1)
711
+ return offset < 0 ? 0 : offset
712
+ }
713
+ }