• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

naver / egjs-grid / 5086275713

pending completion
5086275713

push

github

Daybrush
chore(release): Release 1.14.1

599 of 689 branches covered (86.94%)

Branch coverage included in aggregate %.

1330 of 1362 relevant lines covered (97.65%)

1076.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.85
/src/utils.ts
1
/**
2
 * egjs-grid
3
 * Copyright (c) 2021-present NAVER Corp.
4
 * MIT license
5
 */
6
import Grid from "./Grid";
7
import { GRID_METHODS, GRID_PROPERTY_TYPES, PROPERTY_TYPE } from "./consts";
1✔
8
import { GridItem } from "./GridItem";
9
import { ResizeWatcherEntry } from "./ResizeWatcher";
10
import { diff } from "@egjs/children-differ";
1✔
11

12
export function getKeys<T extends Record<string, any>>(obj: T): Array<keyof T> {
2✔
13
  return Object.keys(obj);
608✔
14
}
15
export function getUpdatedItems(items: GridItem[], entries: ResizeWatcherEntry[]) {
2✔
16
  const mountedItems = getMountedItems(items);
4✔
17

18
  return diff(
4✔
19
    entries.map((entry) => entry.target),
7✔
20
    mountedItems.map((item) => item.element!),
8✔
21
  ).maintained.filter(([prevIndex, nextIndex]) => {
22
    const entrySize = entries[prevIndex].size!;
7✔
23
    const item = items[nextIndex];
7✔
24

25
    return !item.inlineSize || !item.contentSize
7✔
26
      || entrySize.inlineSize !== item.computedInlineSize
27
      || entrySize.blockSize !== item.computedContentSize;
28
  }).map(([, nextIndex]) => items[nextIndex]);
6✔
29
}
30
export function getMountedItems(items: GridItem[]) {
2✔
31
  return items.filter((item) => item.element);
11✔
32
}
33
export function getMountedElements(items: GridItem[]) {
2✔
34
  return getMountedItems(items).map((item) => item.element!);
3✔
35
}
36
export function isString(val: any): val is string {
2✔
37
  return typeof val === "string";
233✔
38
}
39
export function isObject(val: any): val is object {
2✔
40
  return typeof val === "object";
10,575✔
41
}
42
export function isNumber(val: any): val is number {
2✔
43
  return typeof val === "number";
590✔
44
}
45
export function camelize(str: string) {
2✔
46
  return str.replace(/[\s-_]([a-z])/g, (all, letter) => letter.toUpperCase());
58✔
47
}
48

49
export function getDataAttributes(element: HTMLElement, attributePrefix: string) {
2✔
50
  const dataAttributes: Record<string, string> = {};
559✔
51
  const attributes = element.attributes;
559✔
52
  const length = attributes.length;
559✔
53

54
  for (let i = 0; i < length; ++i) {
559✔
55
    const attribute = attributes[i];
568✔
56
    const { name, value } = attribute;
568✔
57
    if (name.indexOf(attributePrefix) === -1) {
568✔
58
      continue;
510✔
59
    }
60
    dataAttributes[camelize(name.replace(attributePrefix, ""))] = value;
58✔
61
  }
62

63
  return dataAttributes;
559✔
64
}
65

66
/* Class Decorator */
67
export function GetterSetter(component: {
2✔
68
  prototype: Grid<any>,
69
  propertyTypes: typeof GRID_PROPERTY_TYPES,
70
}) {
71
  const {
72
    prototype,
6✔
73
    propertyTypes,
74
  } = component;
75
  for (const name in propertyTypes) {
6✔
76
    const shouldRender = propertyTypes[name] === PROPERTY_TYPE.RENDER_PROPERTY;
62✔
77

78
    const descriptor = Object.getOwnPropertyDescriptor(prototype, name) || {};
62✔
79

80
    const getter = descriptor.get || function get(this: Grid) {
62✔
81
      return this.options[name];
21,580✔
82
    };
83
    const setter = descriptor.set || function set(this: Grid, value: any) {
62✔
84
      const options = this.options;
12✔
85
      const prevValue = options[name];
12✔
86

87
      if (prevValue === value) {
12✔
88
        return;
1✔
89
      }
90
      options[name] = value;
11✔
91

92
      if (shouldRender && options.renderOnPropertyChange) {
11✔
93
        this.scheduleRender();
8✔
94
      }
95
    };
96
    const attributes: Record<string, any> = {
62✔
97
      enumerable: true,
98
      configurable: true,
99
      get: getter,
100
      set: setter,
101
    };
102
    Object.defineProperty(prototype, name, attributes);
62✔
103
  }
104
}
105

106
export function withMethods(methods: readonly string[]) {
2✔
107
  return function (prototype: any, memberName: string) {
1✔
108
    methods.forEach((name: string) => {
1✔
109
      if (name in prototype) {
7!
110
        return;
×
111
      }
112
      prototype[name] = function (...args) {
7✔
113
        const result = this[memberName][name](...args);
3✔
114

115
        // fix `this` type to return your own `class` instance to the instance using the decorator.
116
        if (result === this[memberName]) {
3✔
117
          return this;
1✔
118
        } else {
119
          return result;
2✔
120
        }
121
      };
122
    });
123
  };
124
}
125

126
export function range(length: number): number[] {
2✔
127
  const arr: number[] = [];
325✔
128
  for (let i = 0; i < length; ++i) {
325✔
129
    arr.push(i);
667✔
130
  }
131
  return arr;
325✔
132
}
133

134
export function getRangeCost(value: number, valueRange: number[]) {
2✔
135
  return Math.max(value - valueRange[1], valueRange[0] - value, 0) + 1;
17,648✔
136
}
137

138
/**
139
 * Decorator that makes the method of grid available in the framework.
140
 * @ko 프레임워크에서 그리드의 메소드를 사용할 수 있게 하는 데코레이터.
141
 * @memberof eg.Grid
142
 * @private
143
 * @example
144
 * ```js
145
 * import { withGridMethods } from "@egjs/grid";
146
 *
147
 * class Grid extends React.Component<Partial<GridProps & GridOptions>> {
148
 *   &#64;withGridMethods
149
 *   private grid: NativeGrid;
150
 * }
151
 * ```
152
 */
153
export const withGridMethods = withMethods(GRID_METHODS);
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc