11import * as ts from 'typescript' ;
2- import * as kinds from './isKind' ;
3-
4- export * from './isKind' ;
2+ import * as _ from 'lodash' ;
53
64/**
75 * If a class declaration a react class?
@@ -54,7 +52,7 @@ export function isReactComponent(classDeclaration: ts.ClassDeclaration, typeChec
5452export function isReactHeritageClause ( clause : ts . HeritageClause ) {
5553 return clause . token === ts . SyntaxKind . ExtendsKeyword &&
5654 clause . types . length === 1 &&
57- kinds . isExpressionWithTypeArguments ( clause . types [ 0 ] ) &&
55+ ts . isExpressionWithTypeArguments ( clause . types [ 0 ] ) &&
5856 / C o m p o n e n t / . test ( clause . types [ 0 ] . expression . getText ( ) ) ;
5957}
6058
@@ -65,10 +63,10 @@ export function isReactHeritageClause(clause: ts.HeritageClause) {
6563 * @param statement
6664 */
6765export function isReactPropTypeAssignmentStatement ( statement : ts . Statement ) : statement is ts . ExpressionStatement {
68- return kinds . isExpressionStatement ( statement )
69- && kinds . isBinaryExpression ( statement . expression )
66+ return ts . isExpressionStatement ( statement )
67+ && ts . isBinaryExpression ( statement . expression )
7068 && statement . expression . operatorToken . kind === ts . SyntaxKind . FirstAssignment
71- && kinds . isPropertyAccessExpression ( statement . expression . left )
69+ && ts . isPropertyAccessExpression ( statement . expression . left )
7270 && / \. p r o p T y p e s $ | \. p r o p T y p e s \. .+ $ / . test ( statement . expression . left . getText ( ) )
7371}
7472
@@ -80,7 +78,7 @@ export function hasStaticModifier(classMember: ts.ClassElement) {
8078 if ( ! classMember . modifiers ) {
8179 return false ;
8280 }
83- const staticModifier = find ( classMember . modifiers , ( modifier ) => {
81+ const staticModifier = _ . find ( classMember . modifiers , ( modifier ) => {
8482 return modifier . kind == ts . SyntaxKind . StaticKeyword ;
8583 } ) ;
8684 return staticModifier !== undefined ;
@@ -99,49 +97,18 @@ export function isPropTypesMember(classMember: ts.ClassElement, sourceFile: ts.S
9997 }
10098}
10199
102- // TODO: replace following functions with Lodash?
103- // ---------------------------------------------------------------------------------------------------------
104-
105- /**
106- * Find an item in a collection with a matcher
107- * @param collection
108- * @param matcher
109- */
110- export function find < T > ( collection : T [ ] , matcher : ( item : T ) => boolean ) : T | undefined {
111- for ( const item of collection ) {
112- if ( matcher ( item ) ) { return item ; }
113- }
114-
115- return undefined ;
116- }
117-
118- /**
119- * Look in a collection and see if collection has a specific item
120- * @param collection
121- * @param matcher
122- */
123- export function has < T > ( collection : T [ ] , matcher : ( item : T ) => boolean ) : boolean {
124- if ( ! collection || ! collection . length ) {
125- return false ;
126- }
127-
128- for ( const item of collection ) {
129- if ( matcher ( item ) ) { return true ; }
130- }
131-
132- return false ;
133- }
134-
135100/**
136101 * Insert an item in middle of an array after a specific item
137102 * @param collection
138103 * @param afterItem
139104 * @param newItem
140105 */
141- export function insertAfter < T > ( collection : T [ ] , afterItem : T , newItem : T ) {
142- const index = collection . indexOf ( afterItem ) + 1 ;
106+ export function insertAfter < T > ( collection : ArrayLike < T > , afterItem : T , newItem : T ) {
107+ const index = _ . indexOf ( collection , afterItem ) + 1 ;
143108
144- return collection . slice ( 0 , index ) . concat ( newItem ) . concat ( collection . slice ( index ) ) ;
109+ return _ . slice ( collection , 0 , index )
110+ . concat ( newItem )
111+ . concat ( _ . slice ( collection , index ) ) ;
145112}
146113
147114/**
@@ -150,10 +117,12 @@ export function insertAfter<T>(collection: T[], afterItem: T, newItem: T) {
150117 * @param beforeItem
151118 * @param newItem
152119 */
153- export function insertBefore < T > ( collection : T [ ] , beforeItem : T , newItem : T ) {
154- const index = collection . indexOf ( beforeItem ) ;
120+ export function insertBefore < T > ( collection : ArrayLike < T > , beforeItem : T , newItems : T | T [ ] ) {
121+ const index = _ . indexOf ( collection , beforeItem ) ;
155122
156- return collection . slice ( 0 , index ) . concat ( newItem ) . concat ( collection . slice ( index ) ) ;
123+ return _ . slice ( collection , 0 , index )
124+ . concat ( newItems )
125+ . concat ( _ . slice ( collection , index ) ) ;
157126}
158127
159128/**
@@ -162,10 +131,11 @@ export function insertBefore<T>(collection: T[], beforeItem: T, newItem: T) {
162131 * @param item
163132 * @param newItem
164133 */
165- export function replaceItem < T > ( collection : T [ ] , item : T , newItem : T ) {
166- const index = collection . indexOf ( item ) ;
167-
168- return collection . slice ( 0 , index ) . concat ( newItem ) . concat ( collection . slice ( index + 1 ) ) ;
134+ export function replaceItem < T > ( collection : ArrayLike < T > , item : T , newItem : T ) {
135+ const index = _ . indexOf ( collection , item ) ;
136+ return _ . slice ( collection , 0 , index )
137+ . concat ( newItem )
138+ . concat ( _ . slice ( collection , index + 1 ) ) ;
169139}
170140
171141/**
@@ -174,8 +144,7 @@ export function replaceItem<T>(collection: T[], item: T, newItem: T) {
174144 * @param item
175145 * @param newItem
176146 */
177- export function removeItem < T > ( collection : T [ ] , item : T ) {
178- const index = collection . indexOf ( item ) ;
179-
180- return collection . slice ( 0 , index ) . concat ( collection . slice ( index + 1 ) ) ;
147+ export function removeItem < T > ( collection : ArrayLike < T > , item : T ) {
148+ const index = _ . indexOf ( collection , item ) ;
149+ return _ . slice ( collection , 0 , index ) . concat ( _ . slice ( collection , index + 1 ) ) ;
181150}
0 commit comments