web-one 0.0.6 → 0.0.8

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,12 +348,41 @@ 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;
355
355
  }
356
356
  exports.buildSortSearch = buildSortSearch;
357
+ function buildSorts(sorts, prefix) {
358
+ const l = sorts.length;
359
+ for (let i = 0; i < l; i++) {
360
+ sorts[i].value = prefix + sorts[i].value;
361
+ }
362
+ }
363
+ exports.buildSorts = buildSorts;
364
+ function getSortText(sorts, sort, defaultText, textOnly) {
365
+ if (!sort) {
366
+ return defaultText;
367
+ }
368
+ const l = sorts.length;
369
+ if (textOnly) {
370
+ for (let i = 0; i < l; i++) {
371
+ if (sorts[i].value === sort) {
372
+ return sorts[i].text;
373
+ }
374
+ }
375
+ }
376
+ else {
377
+ for (let i = 0; i < l; i++) {
378
+ if (sorts[i].value === sort) {
379
+ return sorts[i].fulltext;
380
+ }
381
+ }
382
+ }
383
+ return defaultText;
384
+ }
385
+ exports.getSortText = getSortText;
357
386
  function formatInteger(v, groupSeparator = ",") {
358
387
  if (v == null || !Number.isFinite(v)) {
359
388
  return "";
@@ -700,3 +729,15 @@ function cloneArray(arr) {
700
729
  return arr.map(item => (Object.assign({}, item)));
701
730
  }
702
731
  exports.cloneArray = cloneArray;
732
+ function getOffset(limit, page, firstLimit) {
733
+ const p = page && page > 0 ? page : 1;
734
+ if (firstLimit && firstLimit > 0) {
735
+ const offset = limit * (p - 2) + firstLimit;
736
+ return offset < 0 ? 0 : offset;
737
+ }
738
+ else {
739
+ const offset = limit * (p - 1);
740
+ return offset < 0 ? 0 : offset;
741
+ }
742
+ }
743
+ 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.8",
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,43 @@ 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
-
339
+ export interface Item {
340
+ id?: string
341
+ value: string
342
+ text?: string
343
+ fulltext?: string
344
+ }
345
+ export function buildSorts(sorts: Item[], prefix: string) {
346
+ const l = sorts.length
347
+ for (let i = 0; i < l; i++) {
348
+ sorts[i].value = prefix + sorts[i].value
349
+ }
350
+ }
351
+ export function getSortText(sorts: Item[], sort: string | undefined, defaultText?: string, textOnly?: boolean): string | undefined {
352
+ if (!sort) {
353
+ return defaultText
354
+ }
355
+ const l = sorts.length
356
+ if (textOnly) {
357
+ for (let i = 0; i < l; i++) {
358
+ if (sorts[i].value === sort) {
359
+ return sorts[i].text
360
+ }
361
+ }
362
+ } else {
363
+ for (let i = 0; i < l; i++) {
364
+ if (sorts[i].value === sort) {
365
+ return sorts[i].fulltext
366
+ }
367
+ }
368
+ }
369
+ return defaultText
370
+ }
340
371
  export function formatInteger(v?: number | null, groupSeparator: string = ","): string {
341
372
  if (v == null || !Number.isFinite(v)) {
342
373
  return ""
@@ -701,3 +732,14 @@ function getChildren(m: Category, all: Category[]) {
701
732
  export function cloneArray<T>(arr: T[]): T[] {
702
733
  return arr.map(item => ({ ...item }));
703
734
  }
735
+
736
+ export function getOffset(limit: number, page?: number, firstLimit?: number): number {
737
+ const p = page && page > 0 ? page : 1
738
+ if (firstLimit && firstLimit > 0) {
739
+ const offset = limit * (p - 2) + firstLimit
740
+ return offset < 0 ? 0 : offset
741
+ } else {
742
+ const offset = limit * (p - 1)
743
+ return offset < 0 ? 0 : offset
744
+ }
745
+ }