How to use the tableschema.infer function in tableschema

To help you get started, we’ve selected a few tableschema examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github datopian / data-cli / lib / utils / datahub.js View on Github external
const steps = resources.map(async res => {
    if (excelFormats.includes(res.descriptor.format)) {
      const buffer = await res.buffer
      const workbook = XLSX.read(buffer, {type: 'buffer', bookSheets: true})
      if (sheets === 'all') {
        sheets = Array.from(Array(workbook.SheetNames.length).keys())
        // We are using sheet idx starting from 1 so we need to add 1 to each idx:
        sheets = sheets.map(value => value + 1)
      } else if (sheets) { // When sheets are indexes of sheets separated by ','
        sheets = sheets.split(',').map(value => parseInt(value))
      } else { // Default case
        sheets = [1]
      }
      for (let i of sheets) {
        const rows = await toArray(await xlsxParser(res, false, i-1))
        const schema = await infer(rows)
        const step = {
          input: res.descriptor.name,
          output: `${res.descriptor.name}-sheet-${i}`,
          tabulator: {
            sheet: i
          },
          schema
        }
        processingSteps.push(step)
      }
    }
  })
  await Promise.all(steps)
github datopian / data-cli / lib / utils / data.js View on Github external
async addSchema() {
    // Ensure resource is tabular
    if (knownTabularFormats.indexOf(this.descriptor.format) === -1) {
      throw new Error('File is not in known tabular format.')
    }
    const rows = await toArray(await this.rows())
    this.descriptor.schema = await infer(rows)
  }
}
github datopian / data-cli / dist / utils / data.js View on Github external
return (0, _asyncToGenerator3.default)(function* () {
      // Ensure resource is tabular
      if (knownTabularFormats.indexOf(_this2.descriptor.format) === -1) {
        throw new Error('File is not in known tabular format.');
      }
      const rows = yield toArray((yield _this2.rows()));
      _this2.descriptor.schema = yield infer(rows);
    })();
  }
github datopian / data.js / lib / index.js View on Github external
this.descriptor.schema = await infer(this.descriptor.data);
      return;
    } // Get parserOptions so we can use it when "infering" schema:


    const parserOptions = await guessParseOptions(this); // We also need to include parserOptions in "dialect" property of descriptor:

    this.descriptor.dialect = {
      delimiter: parserOptions.delimiter,
      quoteChar: parserOptions.quote // Now let's get a stream from file and infer schema:

    };
    let thisFileStream = await this.stream({
      size: 100
    });
    this.descriptor.schema = await infer(thisFileStream, parserOptions);
  }
github datopian / data.js / src / index.js View on Github external
async addSchema() {
    // Ensure file is tabular
    if (knownTabularFormats.indexOf(this.descriptor.format) === -1) {
      throw new Error('File is not in known tabular format.')
    }
    if (this.displayName === 'FileInline') {
      this.descriptor.schema = await infer(this.descriptor.data)
      return
    }
    // Get parserOptions so we can use it when "infering" schema:
    const parserOptions = await guessParseOptions(this)
    // We also need to include parserOptions in "dialect" property of descriptor:
    this.descriptor.dialect = {
      delimiter: parserOptions.delimiter,
      quoteChar: parserOptions.quote
    }
    // Now let's get a stream from file and infer schema:
    let thisFileStream = await this.stream({size: 100})
    this.descriptor.schema = await infer(thisFileStream, parserOptions)
  }
}
github datopian / data.js / lib / index.js View on Github external
async addSchema() {
    // Ensure file is tabular
    if (knownTabularFormats.indexOf(this.descriptor.format) === -1) {
      throw new Error('File is not in known tabular format.');
    }

    if (this.displayName === 'FileInline') {
      this.descriptor.schema = await infer(this.descriptor.data);
      return;
    } // Get parserOptions so we can use it when "infering" schema:


    const parserOptions = await guessParseOptions(this); // We also need to include parserOptions in "dialect" property of descriptor:

    this.descriptor.dialect = {
      delimiter: parserOptions.delimiter,
      quoteChar: parserOptions.quote // Now let's get a stream from file and infer schema:

    };
    let thisFileStream = await this.stream({
      size: 100
    });
    this.descriptor.schema = await infer(thisFileStream, parserOptions);
  }
github datopian / data.js / src / index.js View on Github external
throw new Error('File is not in known tabular format.')
    }
    if (this.displayName === 'FileInline') {
      this.descriptor.schema = await infer(this.descriptor.data)
      return
    }
    // Get parserOptions so we can use it when "infering" schema:
    const parserOptions = await guessParseOptions(this)
    // We also need to include parserOptions in "dialect" property of descriptor:
    this.descriptor.dialect = {
      delimiter: parserOptions.delimiter,
      quoteChar: parserOptions.quote
    }
    // Now let's get a stream from file and infer schema:
    let thisFileStream = await this.stream({size: 100})
    this.descriptor.schema = await infer(thisFileStream, parserOptions)
  }
}
github datopian / datahub-client / lib / utils / datahub.js View on Github external
throw new Error(`sheet index ${sheet} is out of range: please, provide existing sheet index or use sheet name.`)
          } else if (sheet.constructor.name === 'String' && !workbook.SheetNames.includes(sheet)) {
            throw new Error(`sheet name ${sheet} does not exist in the given file.`)
          } else if (sheet.constructor.name === 'String') {
            sheets[idx] = workbook.SheetNames.indexOf(sheet) + 1
          }
        })
      } else { // Default case
        sheets = [1]
      }
      for (let i of sheets) {
        const rows = await toArray(await xlsxParser(res, false, i-1))
        if (rows.length === 0) {
          throw new Error('You cannot push an empty sheet. Please, add some data and try again.')
        }
        const schema = await infer(rows)
        // hacky way to handle types of excel numbers (they actually are floats)
        schema.fields.forEach(field => {
          if (field.type === 'integer') {
            field.type = 'number'
          }
        })
        const step = {
          input: res.descriptor.name,
          output: `${res.descriptor.name}-sheet-${i}`,
          tabulator: {
            sheet: i
          },
          schema
        }
        processingSteps.push(step)
      }
github datopian / data-cli / dist / init.js View on Github external
fs.readFile(path_, (err, data) => {
      if (err) {
        reject(err);
        return;
      }
      const values = parse(data);
      const schema = infer(values);
      resolve(schema);
    });
  });
github datopian / data-cli / lib / init.js View on Github external
fs.readFile(path_, (err, data) => {
      if (err) {
        reject(err)
        return
      }
      const values = parse(data)
      const schema = infer(values)
      resolve(schema)
    })
  })

tableschema

A library for working with Table Schema in Javascript.

MIT
Latest version published 3 years ago

Package Health Score

51 / 100
Full package analysis