Skip to content

Commit

Permalink
refactor: get rid of lodash
Browse files Browse the repository at this point in the history
it drops the number of types generated by 20,000
  • Loading branch information
thetutlage committed Oct 6, 2019
1 parent 118391e commit a4501ee
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 20 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
"@adonisjs/profiler": "^1.2.4",
"@poppinss/traits": "^1.0.0",
"@poppinss/utils": "^2.0.0",
"camelcase": "^5.3.1",
"knex": "^0.19.4",
"knex-dynamic-connection": "^1.0.0",
"lodash": "^4.17.15",
"pluralize": "^8.0.0",
"snake-case": "^2.1.0",
"ts-essentials": "^3.0.2"
Expand All @@ -59,7 +59,6 @@
"@adonisjs/sink": "^2.2.1",
"@poppinss/dev-utils": "^1.0.1",
"@types/dotenv": "^6.1.1",
"@types/lodash": "^4.14.141",
"@types/node": "^12.7.11",
"@types/pluralize": "0.0.29",
"clone": "^2.1.2",
Expand Down
4 changes: 2 additions & 2 deletions src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// <reference path="../../../adonis-typings/index.ts" />

import pluralize from 'pluralize'
import { isObject, snakeCase } from 'lodash'
import snakeCase from 'snake-case'
import { Exception } from '@poppinss/utils'

import { QueryClientContract, TransactionClientContract } from '@ioc:Adonis/Lucid/Database'
Expand All @@ -32,11 +32,11 @@ import {

import { Preloader } from '../Preloader'
import { HasOne } from '../Relations/HasOne'
import { ensureRelation } from '../../utils'
import { proxyHandler } from './proxyHandler'
import { HasMany } from '../Relations/HasMany'
import { BelongsTo } from '../Relations/BelongsTo'
import { ManyToMany } from '../Relations/ManyToMany'
import { ensureRelation, isObject } from '../../utils'
import { HasManyThrough } from '../Relations/HasManyThrough'

function StaticImplements<T> () {
Expand Down
4 changes: 2 additions & 2 deletions src/Orm/Relations/BelongsTo/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
/// <reference path="../../../../adonis-typings/index.ts" />

import knex from 'knex'
import { uniq } from 'lodash'
import { Exception } from '@poppinss/utils'

import { ModelContract, BelongsToQueryBuilderContract } from '@ioc:Adonis/Lucid/Model'
import { QueryClientContract, TransactionClientContract } from '@ioc:Adonis/Lucid/Database'

import { BelongsTo } from './index'
import { unique } from '../../../utils'
import { BaseRelationQueryBuilder } from '../Base/QueryBuilder'

/**
Expand Down Expand Up @@ -58,7 +58,7 @@ export class BelongsToQueryBuilder
* Constraint for multiple parents
*/
if (Array.isArray(this._parent)) {
const values = uniq(this._parent.map((parentInstance) => {
const values = unique(this._parent.map((parentInstance) => {
return this.$getRelatedValue(parentInstance, this._relation.foreignKey)
}))
return this.whereIn(this._relation.localAdapterKey, values)
Expand Down
3 changes: 2 additions & 1 deletion src/Orm/Relations/BelongsTo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
/// <reference path="../../../../adonis-typings/index.ts" />

import { Exception } from '@poppinss/utils'
import { camelCase, snakeCase } from 'lodash'
import snakeCase from 'snake-case'
import camelCase from 'camelcase'

import {
ModelContract,
Expand Down
4 changes: 2 additions & 2 deletions src/Orm/Relations/HasMany/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
/// <reference path="../../../../adonis-typings/index.ts" />

import knex from 'knex'
import { uniq } from 'lodash'
import { HasManyQueryBuilderContract, ModelContract } from '@ioc:Adonis/Lucid/Model'
import { QueryClientContract, TransactionClientContract } from '@ioc:Adonis/Lucid/Database'

import { HasMany } from './index'
import { unique } from '../../../utils'
import { BaseRelationQueryBuilder } from '../Base/QueryBuilder'

/**
Expand Down Expand Up @@ -56,7 +56,7 @@ export class HasManyQueryBuilder
* Constraint for multiple parents
*/
if (Array.isArray(this._parent)) {
const values = uniq(this._parent.map((parentInstance) => {
const values = unique(this._parent.map((parentInstance) => {
return this.$getRelatedValue(parentInstance, this._relation.localKey)
}))
return this.whereIn(this._relation.foreignAdapterKey, values)
Expand Down
4 changes: 2 additions & 2 deletions src/Orm/Relations/HasManyThrough/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
/// <reference path="../../../../adonis-typings/index.ts" />

import knex from 'knex'
import { uniq } from 'lodash'
import { Exception } from '@poppinss/utils'
import { HasManyThroughQueryBuilderContract, ModelContract } from '@ioc:Adonis/Lucid/Model'
import { QueryClientContract } from '@ioc:Adonis/Lucid/Database'

import { HasManyThrough } from './index'
import { unique } from '../../../utils'
import { BaseRelationQueryBuilder } from '../Base/QueryBuilder'

/**
Expand Down Expand Up @@ -76,7 +76,7 @@ export class HasManyThroughQueryBuilder
* Constraint for multiple parents
*/
if (Array.isArray(this._parent)) {
const values = uniq(this._parent.map((parentInstance) => {
const values = unique(this._parent.map((parentInstance) => {
return this.$getRelatedValue(parentInstance, this._relation.localKey)
}))
return this.whereIn(`${throughTable}.${this._relation.foreignAdapterKey}`, values)
Expand Down
3 changes: 2 additions & 1 deletion src/Orm/Relations/HasManyThrough/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
/// <reference path="../../../../adonis-typings/index.ts" />

import { Exception } from '@poppinss/utils'
import { camelCase, snakeCase } from 'lodash'
import snakeCase from 'snake-case'
import camelCase from 'camelcase'

import {
ModelContract,
Expand Down
4 changes: 2 additions & 2 deletions src/Orm/Relations/HasOne/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
/// <reference path="../../../../adonis-typings/index.ts" />

import knex from 'knex'
import { uniq } from 'lodash'
import { Exception } from '@poppinss/utils'
import { HasOneQueryBuilderContract, ModelContract } from '@ioc:Adonis/Lucid/Model'
import { QueryClientContract, TransactionClientContract } from '@ioc:Adonis/Lucid/Database'

import { HasOne } from './index'
import { unique } from '../../../utils'
import { BaseRelationQueryBuilder } from '../Base/QueryBuilder'

/**
Expand Down Expand Up @@ -54,7 +54,7 @@ export class HasOneQueryBuilder extends BaseRelationQueryBuilder implements HasO
* Constraint for multiple parents
*/
if (Array.isArray(this._parent)) {
const values = uniq(this._parent.map((parentInstance) => {
const values = unique(this._parent.map((parentInstance) => {
return this.$getRelatedValue(parentInstance, this._relation.localKey)
}))
return this.whereIn(this._relation.foreignAdapterKey, values)
Expand Down
3 changes: 2 additions & 1 deletion src/Orm/Relations/HasOneOrMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
/// <reference path="../../../adonis-typings/index.ts" />

import { Exception } from '@poppinss/utils'
import { camelCase, snakeCase } from 'lodash'
import snakeCase from 'snake-case'
import camelCase from 'camelcase'

import {
ModelContract,
Expand Down
6 changes: 3 additions & 3 deletions src/Orm/Relations/ManyToMany/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
/// <reference path="../../../../adonis-typings/index.ts" />

import knex from 'knex'
import { uniq, difference } from 'lodash'
import { ModelContract, ManyToManyQueryBuilderContract } from '@ioc:Adonis/Lucid/Model'
import { QueryClientContract, TransactionClientContract } from '@ioc:Adonis/Lucid/Database'

import { ManyToMany } from './index'
import { unique, difference } from '../../../utils'
import { BaseRelationQueryBuilder } from '../Base/QueryBuilder'

/**
Expand Down Expand Up @@ -249,7 +249,7 @@ export class ManyToManyQueryBuilder
* Constraint for multiple parents
*/
if (Array.isArray(this._parent)) {
const values = uniq(this._parent.map((parentInstance) => {
const values = unique(this._parent.map((parentInstance) => {
return this.$getRelatedValue(parentInstance, this._relation.localKey)
}))
return this.whereInPivot(this._relation.pivotForeignKey, values)
Expand Down Expand Up @@ -348,7 +348,7 @@ export class ManyToManyQueryBuilder
ids: (string | number)[] | { [key: string]: any },
checkExisting: boolean,
) {
let idsList = uniq(Array.isArray(ids) ? ids : Object.keys(ids))
let idsList = unique(Array.isArray(ids) ? ids : Object.keys(ids))
const hasAttributes = !Array.isArray(ids)

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Orm/Relations/ManyToMany/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// <reference path="../../../../adonis-typings/index.ts" />

import { Exception } from '@poppinss/utils'
import { snakeCase, sortBy } from 'lodash'
import snakeCase from 'snake-case'

import {
ModelContract,
Expand Down Expand Up @@ -156,7 +156,7 @@ export class ManyToMany implements RelationContract {
}

this.pivotTable = this._options.pivotTable || snakeCase(
sortBy([this.relatedModel().name, this.model.name]).join('_'),
[this.relatedModel().name, this.model.name].sort().join('_'),
)

/**
Expand Down
15 changes: 15 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,18 @@ export function getValue (

return value
}

export function isObject (value: any): boolean {
return value !== null && typeof (value) === 'object' && !Array.isArray(value)
}

export function unique (value: any[]) {
if (!Array.isArray(value)) {
return []
}
return [...new Set(value)]
}

export function difference (main: any[], other: []) {
return [main, other].reduce((a, b) => a.filter(c => !b.includes(c)))
}

0 comments on commit a4501ee

Please sign in to comment.