Last active
August 9, 2018 00:55
-
-
Save will-wow/16c1fd80f759c9bb62a4051a0a3d6c0e to your computer and use it in GitHub Desktop.
Using Elixir-Style Modules in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from ‘react’; | |
import {userType, fullName} from ‘./user’; | |
const UserComponent = user => ( | |
<div>Name: {fullName(user)}</div> | |
); | |
UserComponent.propTypes = { | |
user: userType | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from ‘react’; | |
import * as User from ‘./user’; | |
const UserComponent = user => ( | |
<div>Name: {User.fullName(user)}</div> | |
); | |
UserComponent.propTypes = { | |
user: User.t | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as User from './user'; | |
class UserComponent { | |
name(): User.t { | |
return User.fullName(this.user); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { userToFriend } from 'accounts/User'; | |
userToFriend(user): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const userType = PropTypes.shape({ | |
firstName: PropTypes.string; | |
lastName: PropTypes.string; | |
email: PropTypes.string; | |
}); | |
export function fullName(user) { | |
return `${user.firstName} ${user.lastName}`; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as User from 'accounts/User'; | |
User.toFriend(user): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule User do | |
defstruct [:first_name, :last_name, :email] | |
def full_name(user = %User{}) do | |
"#{user.first_name} #{user.last_name} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @typedef {Object} User | |
* @property {string} firstName | |
* @property {string} lastName | |
* @property {string} email | |
*/ | |
/** | |
* @param {User} user | |
* @returns {string} | |
*/ | |
export function fullName(user) { | |
return `${user.firstName} ${user.lastName}`; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface User { | |
firstName: string; | |
lastName: string; | |
email: string; | |
} | |
export function fullName(user: User): string { | |
return `${user.firstName} ${user.lastName}`; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const t = PropTypes.shape({ | |
firstName: PropTypes.string; | |
lastName: PropTypes.string; | |
email: PropTypes.string; | |
}); | |
export function fullName(user) { | |
return `${user.firstName} ${user.lastName}`; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface t { | |
firstName: string; | |
lastName: string; | |
email: string; | |
} | |
export function fullName(user: t): string { | |
return `${user.firstName} ${user.lastName}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment