[
  {
    "path": ".gitignore",
    "content": "node_modules\nyarn-error.log\ndist\ncoverage\n.DS_Store"
  },
  {
    "path": ".npmignore",
    "content": ".gitignore\ntsconfig.json\nindex.ts\nindex.spec.js\ncoverage\n.DS_Store"
  },
  {
    "path": "LICENSE",
    "content": "Copyright (c) 2017 Jan Soendermann\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); \nyou may not use this file except in compliance with the License. \nYou may obtain a copy of the License at\n\nhttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software \ndistributed under the License is distributed on an \"AS IS\" BASIS, \nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. \nSee the License for the specific language governing permissions and \nlimitations under the License."
  },
  {
    "path": "README.md",
    "content": "# React Native SectionList getItemLayout\n\nThis package provides a function that helps you construct the `getItemLayout` function for your `SectionList`s. For an explanation of why this exists, see [this post](https://medium.com/@jsoendermann/sectionlist-and-getitemlayout-2293b0b916fb). It's meant to be used like this:\n\n```javascript\nimport sectionListGetItemLayout from 'react-native-section-list-get-item-layout'\n\nclass MyComponent extends React.Component {\n  constructor(props) {\n    super(props)\n\n    this.getItemLayout = sectionListGetItemLayout({\n      // The height of the row with rowData at the given sectionIndex and rowIndex\n      getItemHeight: (rowData, sectionIndex, rowIndex) => sectionIndex === 0 ? 100 : 50,\n\n      // These four properties are optional\n      getSeparatorHeight: () => 1 / PixelRatio.get(), // The height of your separators\n      getSectionHeaderHeight: () => 20, // The height of your section headers\n      getSectionFooterHeight: () => 10, // The height of your section footers\n      listHeaderHeight: 40, // The height of your list header\n    })\n  }\n\n  render() {\n    return (\n      <SectionList\n        {...otherStuff}\n        getItemLayout={this.getItemLayout}\n      />\n    )\n  }\n}\n```\n"
  },
  {
    "path": "index.spec.js",
    "content": "const sectionListGetItemLayout = require('./dist').default\n\ntest('Empty sections', () => {\n  const getItemLayout = sectionListGetItemLayout({\n    getItemHeight: () => 2,\n    getSeparatorHeight: () => 23,\n    getSectionHeaderHeight: () => 41,\n    getSectionFooterHeight: () => 61,\n  })\n\n  const data = [{ data: [] }, { data: [] }, { data: [null] }]\n\n  expect(getItemLayout(data, 0)).toEqual({ length: 41, offset: 0, index: 0 })\n  expect(getItemLayout(data, 1)).toEqual({ length: 61, offset: 41, index: 1 })\n  expect(getItemLayout(data, 5)).toEqual({\n    length: 2,\n    offset: 41 * 3 + 61 * 2,\n    index: 5,\n  })\n  expect(getItemLayout(data, 6)).toEqual({\n    length: 61,\n    offset: 41 * 3 + 61 * 2 + 2,\n    index: 6,\n  })\n})\n\ntest('Multiple rows in one section', () => {\n  const getItemLayout = sectionListGetItemLayout({\n    getItemHeight: () => 2,\n    getSeparatorHeight: () => 23,\n    getSectionHeaderHeight: () => 41,\n    getSectionFooterHeight: () => 61,\n  })\n\n  const data = [{ data: [null, null, null] }]\n\n  expect(getItemLayout(data, 2)).toEqual({\n    length: 2,\n    offset: 41 + 2 + 23,\n    index: 2,\n  })\n})\n\ntest('Calling sectionListGetItemLayout with only getItemHeight', () => {\n  const getItemLayout = sectionListGetItemLayout({\n    getItemHeight: () => 1,\n  })\n\n  const data = [{ data: [null, null] }]\n\n  expect(getItemLayout(data, 0)).toEqual({ length: 0, offset: 0, index: 0 })\n  expect(getItemLayout(data, 1)).toEqual({ length: 1, offset: 0, index: 1 })\n  expect(getItemLayout(data, 2)).toEqual({ length: 1, offset: 1, index: 2 })\n  expect(getItemLayout(data, 3)).toEqual({ length: 0, offset: 2, index: 3 })\n})\n"
  },
  {
    "path": "index.ts",
    "content": "export type SectionListDataProp = Array<{\n  title: string\n  data: any[]\n}>\n\ninterface SectionHeader {\n  type: 'SECTION_HEADER'\n}\n\ninterface Row {\n  type: 'ROW'\n  index: number\n}\n\ninterface SectionFooter {\n  type: 'SECTION_FOOTER'\n}\n\ntype ListElement = SectionHeader | Row | SectionFooter\n\nexport interface Parameters {\n  getItemHeight: (\n    rowData: any,\n    sectionIndex: number,\n    rowIndex: number,\n  ) => number\n  getSeparatorHeight?: (sectionIndex: number, rowIndex: number) => number\n  getSectionHeaderHeight?: (sectionIndex: number) => number\n  getSectionFooterHeight?: (sectionIndex: number) => number\n  listHeaderHeight?: number | (() => number)\n}\n\nexport default ({\n  getItemHeight,\n  getSeparatorHeight = () => 0,\n  getSectionHeaderHeight = () => 0,\n  getSectionFooterHeight = () => 0,\n  listHeaderHeight = 0,\n}: Parameters) => (data: SectionListDataProp, index: number) => {\n  let i = 0\n  let sectionIndex = 0\n  let elementPointer: ListElement = { type: 'SECTION_HEADER' }\n  let offset =\n    typeof listHeaderHeight === 'function'\n      ? listHeaderHeight()\n      : listHeaderHeight\n\n  while (i < index) {\n    switch (elementPointer.type) {\n      case 'SECTION_HEADER': {\n        const sectionData = data[sectionIndex].data\n\n        offset += getSectionHeaderHeight(sectionIndex)\n\n        // If this section is empty, we go right to the footer...\n        if (sectionData.length === 0) {\n          elementPointer = { type: 'SECTION_FOOTER' }\n          // ...otherwise we make elementPointer point at the first row in this section\n        } else {\n          elementPointer = { type: 'ROW', index: 0 }\n        }\n\n        break\n      }\n      case 'ROW': {\n        const sectionData = data[sectionIndex].data\n\n        const rowIndex = elementPointer.index\n\n        offset += getItemHeight(sectionData[rowIndex], sectionIndex, rowIndex)\n        elementPointer.index += 1\n\n        if (rowIndex === sectionData.length - 1) {\n          elementPointer = { type: 'SECTION_FOOTER' }\n        } else {\n          offset += getSeparatorHeight(sectionIndex, rowIndex)\n        }\n\n        break\n      }\n      case 'SECTION_FOOTER': {\n        offset += getSectionFooterHeight(sectionIndex)\n        sectionIndex += 1\n        elementPointer = { type: 'SECTION_HEADER' }\n        break\n      }\n    }\n\n    i += 1\n  }\n\n  let length\n  switch (elementPointer.type) {\n    case 'SECTION_HEADER':\n      length = getSectionHeaderHeight(sectionIndex)\n      break\n    case 'ROW':\n      const rowIndex = elementPointer.index\n      length = getItemHeight(\n        data[sectionIndex].data[rowIndex],\n        sectionIndex,\n        rowIndex,\n      )\n      break\n    case 'SECTION_FOOTER':\n      length = getSectionFooterHeight(sectionIndex)\n      break\n    default:\n      throw new Error('Unknown elementPointer.type')\n  }\n\n  return { length, offset, index }\n}\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"react-native-section-list-get-item-layout\",\n  \"version\": \"2.2.3\",\n  \"description\":\n    \"This package simplifies constructing the getItemLayout prop for react native SectionLists.\",\n  \"main\": \"./dist/index.js\",\n  \"typings\": \"./dist/index.d.ts\",\n  \"license\": \"Apache-2.0\",\n  \"author\": {\n    \"name\": \"Jan Soendermann\",\n    \"email\": \"jan.soendermann+npm@gmail.com\"\n  },\n  \"scripts\": {\n    \"ts:build\": \"tsc\",\n    \"ts:watch\": \"tsc --watch\",\n    \"test\": \"jest\",\n    \"prepublish\": \"npm run ts:build\"\n  },\n  \"devDependencies\": {\n    \"jest\": \"^22.4.4\",\n    \"typescript\": \"^2.8.3\"\n  }\n}\n"
  },
  {
    "path": "tsconfig.json",
    "content": "{\n    \"compilerOptions\": {\n        \"target\": \"es3\",\n        \"strict\": true,\n        \"sourceMap\": true,\n        \"declaration\": true,\n        \"outDir\": \"dist\"\n    },\n    \"include\": [\n        \"index.ts\"\n    ]\n}"
  }
]